leetcode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)

目录

  • 题目描述:
  • 示例:
  • 解法:

题目描述:

给定一个二叉树,原地将它展开为链表。

示例:

给定二叉树

    1
   /   2   5
 / \   3   4   6

将其展开为:

1
   2
       3
           4
               5
                   6

解法:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    void flatten(TreeNode* root, TreeNode*& head, TreeNode*& tail){
        if(root == NULL){
            return;
        }else{
            head = root;
            TreeNode* lhead = NULL, * ltail = NULL;
            TreeNode* rhead = NULL, * rtail = NULL;
            if(root->left == NULL && root->right == NULL){
                // leaf node
                tail = root;
            }else if(root->left == NULL){
                flatten(root->right, rhead, rtail);
                tail = rtail;
                root->left = NULL;
                root->right = rhead;
            }else if(root->right == NULL){
                flatten(root->left, lhead, ltail);
                tail = ltail;
                root->left = NULL;
                root->right = lhead;
            }else{
                flatten(root->left, lhead, ltail);
                flatten(root->right, rhead, rtail);
                ltail->left = NULL;
                ltail->right = rhead;
                tail = rtail;
                root->left = NULL;
                root->right = lhead;
            }
        }
    }

    void flatten(TreeNode* root) {
        TreeNode* head = NULL, * tail = NULL;
        flatten(root, head, tail);
        root = head;
    }
};

原文地址:https://www.cnblogs.com/zhanzq/p/10794450.html

时间: 2024-11-06 09:37:09

leetcode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)的相关文章

leetcode -day17 Path Sum I II & Flatten Binary Tree to Linked List & Minimum Depth of Binary Tree

1.  Path Sum Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. For example: Given the below binary tree and sum = 22, 5 / 4 8 / / 11 13 4 / \ 7 2 1 r

[LeetCode] 114. 二叉树展开为链表 ☆☆☆(深度遍历)

二叉树展开为链表(很详细) 描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \3 4 6将其展开为: 1 \ 2 \ 3 \ 4 \ 5 \ 6 解析 变形的后续遍历 直觉是先序遍历,但是节点会丢失,可以使用后续遍历. 我们依次遍历 6 5 4 3 2 1,然后每遍历一个节点就将当前节点的右指针更新为上一个节点. 遍历到 5,把 5 的右指针指向 6.6 <- 5 4 3 2 1. 遍历到 4,把 4 的右指针指向 5.6 <- 5 <- 4 3

LeetCode 114. 二叉树展开为链表(Flatten Binary Tree to Linked List)

题目描述 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / 2 5 / \ 3 4 6 将其展开为: 1 2 3 4 5 6 解题思路 二叉树转化为链表的基本思想是:对于左孩子转化为右孩子:对于右孩子,拼接到根结点左子树最后一个节点作为右孩子.所以在自上而下转化时,对于每个节点要先保存其右孩子,然后记录转为链表后本子树的最后一个节点并返回给上一个根节点. 代码 1 /** 2 * Definition for a binary tree node. 3 * struct TreeN

leetcode 114. 二叉树展开为链表(dfs)

给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / \ 2 5 / \ \3 4 6将其展开为: 1 \    2    \ 3 \ 4 \ 5 \ 6 来源:力扣(LeetCode)链接:https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNod

[LeetCode] 114. 二叉树展开为链表

题目链接 : https://leetcode-cn.com/problems/flatten-binary-tree-to-linked-list/ 题目描述: 给定一个二叉树,原地将它展开为链表. 示例: 例如,给定二叉树 1 / 2 5 / \ 3 4 6 将其展开为: 1 2 3 4 5 6 思路: 其实对于这种题目,递归不太好想的,可以有个取巧的方法,就是把树转列表,因为结果是按照前序遍历的,所以有: def flatten(self, root: TreeNode) -> None:

LeetCode: Flatten Binary Tree to Linked List [114]

[题目] Given a binary tree, flatten it to a linked list in-place. For example, Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6 click to show hints. [题意] 将一颗二叉树转化为链表,right充当next指针,元素顺序为先序遍历的循序.不使用额外的空间 [思路] 先对左子树链表化,然后将右子树链表化.然后

114 Flatten Binary Tree to Linked List [Python]

114 Flatten Binary Tree to Linked List Given a binary tree, flatten it to a linked list in-place. 将二叉树展开成链表 [[]D:\dataStructure\Leetcode\114.png] (D:\dataStructure\Leetcode\114.png "114") 思路:将根节点与左子树相连,再与右子树相连.递归地在每个节点的左右孩子节点上,分别进行这样的操作. 代码 clas

【LeetCode】Flatten Binary Tree to Linked List

题目:Flatten Binary Tree to Linked List <span style="font-size:18px;">/**LeetCode Flatten Binary Tree to Linked List * 题意:给定一个二叉树,将其转变为一个相当于单链表的结构,观察可知该结构即:每一个节点左儿子为空,右儿子指向自己先序遍历时的下一个节点 * 思路:有观察可得,应对其进行先序遍历,得到正确的序列连接起来 * Definition for binar

[leetcode]Flatten Binary Tree to Linked List @ Python

原题地址:http://oj.leetcode.com/problems/flatten-binary-tree-to-linked-list/ 题意: Given a binary tree, flatten it to a linked list in-place. For example,Given 1 / 2 5 / \ 3 4 6 The flattened tree should look like: 1 2 3 4 5 6Hints: If you notice carefully