[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

Hints:

If you notice carefully in the flattened tree, each node‘s right child points to the next node of a pre-order traversal.

题意:将任意一颗二叉树转换成一颗仅有左子树的二叉树。

要求:(1)不得使用额外存储空间,就地排序整理。

(2)修改之后的树为仅有左子树的二叉树,节点顺序为原二叉树的先序遍历。

思路:说是结果为先序遍历,但是递归方式为后序遍历方式。

class Solution {
public:
    void flatten(TreeNode *root) {
        if(root==NULL) return;
        flatten(root->left);
        flatten(root->right);
        if(root->left==NULL) return;
        TreeNode *p = root->left;
        while(p->right) p=p->right;
        p->right=root->right;
        root->right=root->left;
        root->left=NULL;
    }
};

转载请注明出处http://www.cnblogs.com/double-win/ 谢谢!

[LeetCode 题解]: Flatten Binary Tree to Linked List

时间: 2024-10-06 22:55:05

[LeetCode 题解]: Flatten Binary Tree to Linked List的相关文章

【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 dfs Flatten Binary Tree to Linked List

Flatten Binary Tree to Linked List Total Accepted: 25034 Total Submissions: 88947My Submissions 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

leetcode 之 Flatten Binary Tree to Linked List

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 思路:对二叉树进行先序遍历,把左子树为空的节点的左子树指向它的后序节点,然后把所有的右子树指向左子树 struct TreeNode { i

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

leetCode(42):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 If you notice carefully in the flattened tree, each node's right child points to the next node of a pre-o

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 (15) Flatten Binary Tree to Linked List

题目描述 Given a binary tree, flatten it to a linked list in-place. For example, Given The flattened tree should look like: 本题也是考察二叉树和指针操作的题目.题目要求将一棵二叉树拉平为一个链表 .链表通过树节点的右子树相连,且展开的顺序为原来树的前序遍历. 实现思路: 若节点n存在左子树left,则将左子树连接为n的右子树 若n存在右子树(n->right存在),则将n->ri

[C++]LeetCode: 102 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 po

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