8.7 114 Flatten Binary Tree to Linked List

Question:

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

Method: put root.left to its right, and connect root.right to its original left.right(if root.left.right == null then connect root.right to root.left)
    public void flatten(TreeNode root) {
        while(root != null) {
            if(root.left != null) {
                TreeNode lr = root.left.right;
                while(lr != null && lr.right != null)
                    lr = lr.right;
                if(lr == null) // if left has no right nodes then use root.left to connect root.right.
                    lr = root.left;
                TreeNode temp = root.right;
                root.right = root.left;
                lr.right = temp;
                root.left = null;
            }
            root = root.right;
        }
    }
时间: 2024-08-10 14:54:00

8.7 114 Flatten Binary Tree to Linked List的相关文章

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

刷题114. Flatten Binary Tree to Linked List

一.题目说明 题目114. Flatten Binary Tree to Linked List,将一个二叉树"原地"压缩为"链表"形态的二叉树.难度为Medium! 二.我的解答 这个题目如果允许使用栈的话Easy,先序遍历二叉树,右子树入栈,左子树入栈.当栈不空的时候,将栈顶元素放到右子树即可. class Solution{ public: void flatten(TreeNode* root){ //先根遍历 if(root==NULL) return;

!!!!!!114. 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 ======== 思路: 将一个二叉树 就地 压成"链表"的结构;

Java for LeetCode 114 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解题思路:试图通过排序后new TreeNode是无法通过的,这道题的意思是把现有的树进行剪枝操作.JAVA实现如下: static public void flatten(TreeNode root) { wh

LeetCode OJ 114. 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. Subscribe to see which companies asked this question 解答 先序遍历同时把节点都堆到左边,因为先序先处理左子树,所以这

leetcode 114. Flatten Binary Tree to Linked List (Python版)

题目: Given a binary tree, flatten it to a linked list in-place. 算法思路: 其实该题目就是二叉树前序遍历的变形 我代码沿用leetcode 144. Binary Tree Preorder Traversal 代码: class Solution(object):     def preorderTraversal(self, root):         """         :type root: Tree

114. Flatten Binary Tree to Linked List (Stack, Tree; DFS)

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 法I:堆栈 struct TreeNode { int val; TreeNode *left; TreeNode *right; TreeNode(int x) : val(x), left(NULL), r

[LeetCode] 114. Flatten Binary Tree to Linked List Java

题目: 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 题意及分析:给出一棵树,要求给出这棵树的先序遍历组成的链表,但是还是用树表示.首先找到根节点左子节点的最右子节点,然后将根节点的右子树移到该点的右节点上:再将根节点的左子节点移到根节点的右子节点上,并将根

【一天一道LeetCode】#114. Flatten Binary Tree to Linked List

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 (二