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-order traversal.

/**
 * 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)
    		return;
    	TreeNode* rightNode = root->right;//保存右孩子
    	TreeNode* tmp = root->left;
    	while (tmp && tmp->right)
    	{
    		tmp = tmp->right;
    	}//找到右子的直接前驱

    	if (tmp)//如果左孩子不为空
    	{
    		tmp->right = rightNode;//让直接前驱指向保存的右孩子
    		root->right = root->left;//右指针指向左孩子
    		root->left = NULL;//左指针指向空
    		flatten(root->right);//对左孩子作同样的处理
    	}
    	flatten(rightNode);//对右孩子作同样的处理
    }
};

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-24 04:07:30

leetCode(42):Flatten Binary Tree to Linked List的相关文章

[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

【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

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