[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:
        """
        Do not return anything, modify root in-place instead.
        """
        if not root:return root
        d = []
        def helper(root):
            if not root:
                return
            d.append(root.val)
            helper(root.left)
            helper(root.right)
        helper(root)
        i = 1
        root.left = None
        p = root
        while i < len(d):
            p.right = TreeNode(d[i])
            p = p.right
            i += 1

上面做法属于作弊过的,

思路一: 递归, 类似后序遍历

思路二: 迭代,

直接看代码,很容易理解,但是不容易想!

代码:

思路一:

def flatten(self, root: TreeNode, pre = None) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        # 类似后序遍历
        def helper(root, pre):
            if not root: return pre
            # 记录遍历时候,该节点的前一个节点
            pre = helper(root.right, pre)
            pre = helper(root.left, pre)
            # 拼接
            root.right = pre
            root.left = None
            pre = root
            return pre
        helper(root, None)

java

class Solution {
    public void flatten(TreeNode root) {
        helper(root, null);
    }

    private TreeNode helper(TreeNode root, TreeNode pre) {
        if (root == null) return pre;
        pre = helper(root.right, pre);
        pre = helper(root.left, pre);
        root.right = pre;
        root.left = null;
        pre = root;
        return pre;
    }
}

思路二:

def flatten(self, root: TreeNode) -> None:
        """
        Do not return anything, modify root in-place instead.
        """
        cur = root
        while cur:
            if cur.left:
                p = cur.left
                while p.right: p = p.right
                p.right = cur.right
                cur.right = cur.left
                cur.left = None
            cur = cur.right

java

class Solution {
    public void flatten(TreeNode root) {
      TreeNode cur = root;
        while (cur != null) {
            if (cur.left != null) {
                TreeNode p = cur.left;
                while (p.right != null) p = p.right;
                p.right = cur.right;
                cur.right = cur.left;
                cur.left = null;
            }
            cur = cur.right;
        }
    }
}

?

原文地址:https://www.cnblogs.com/powercai/p/11116688.html

时间: 2024-11-06 09:35:51

[LeetCode] 114. 二叉树展开为链表的相关文章

[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. 二叉树展开为链表(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. 二叉树展开为链表(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. 二叉树展开为链表(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(NU

114. 二叉树展开为链表

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 11 class Solution 12 { 13 public: 14 void flatten(TreeNod

LeetCode(114): 二叉树展开为链表

Medium! 题目描述: 给定一个二叉树,原地将它展开为链表. 例如,给定二叉树 1 / 2 5 / \ 3 4 6 将其展开为: 1 2 3 4 5 6 解题思路: 这道题要求把二叉树展开成链表,根据展开后形成的链表的顺序分析出是使用先序遍历,那么只要是数的遍历就有递归和非递归的两种方法来求解,这里我们也用两种方法来求解. 首先来看递归版本的,思路是先利用DFS的思路找到最左子节点,然后回到其父节点,把其父节点和右子节点断开,将原左子结点连上父节点的右子节点上,然后再把原右子节点连到新右子节

[LeetCode] 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 6 click to show hints. Hints: If you notice carefully in the flattened tree, each node's right child points

[LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the right pointer in TreeNode as the next pointer in ListNode. Notice Don't forget to mark the left child of each node to null. Or you will get Time Limit Exceeded

leecode第一百一十四题(二叉树展开为链表)

/** * 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) { if(root==NULL)//记住!roo