【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 binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
package javaTrain;

public class Train18 {
	public void flatten(TreeNode root) {
		if(root== null || (root.left == null && root.right == null)) return;
		preOrder(root);
		return;
    }
	private TreeNode preOrder(TreeNode root){
		if(root== null || (root.left == null && root.right == null)) return root;
		TreeNode left = root.left;
		TreeNode right = root.right;
		TreeNode last;
		root.left = null;
		if(left != null){
			root.right = left;
			last = preOrder(left);
			last.left = null;
			if(right != null){
				last.right = right;
				return preOrder(right);
			}
			else return last;
		}
		else{
			root.right = right;
			return preOrder(right);
		}
	}
}
</span>
时间: 2024-10-03 22:49:38

【LeetCode】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 [解析] 题意是把一棵二叉树按照先序遍历的方式放到一棵只有右支树的二叉树中. 最开始想的思路是递归发,后来发现这样会溢出. 然后就用一个栈和一个队列来实现,队列用来存储先序遍历的结果,栈用于先序遍历.

【leetcode】Flatten Binary Tree to Linked List (middle)

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 思路: 用先序遍历,得到的是从小到大的顺序.把先序遍历变形一下: void flatten(TreeNode* root) { if(NULL == root) return; vector<TreeNode *

【Leetcode】【Medium】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 解题思路: 看上去很简单,使用二叉树的中序遍历就可以实现.但是题目的小曲点在于要在原tree上修改,如果将左儿子放在右儿子位置上,会丢失右子树,导致遍历失败. 解决方法有两种: 1.不使用多余空间,将右子树放在左

【树】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 思路: 按照树的先序遍历顺序把节点串联起来即可. /** * Definition for a binary tree node. * function TreeNode(val) { * this.va

【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】Construct Binary Tree from Preorder and Inorder Traversal

问题: 给定二叉树的前序和中序遍历,重构这课二叉树. 分析: 前序.中序.后序都是针对于根结点而言,所以又叫作先根.中根.后根(当然不是高跟). 前序:根  左 右 中序:左  根 右 对二叉树,我们将其进行投影,就会发现个有趣的事: 发现投影后的顺序,恰好是中序遍历的顺序,这也就是为什么在构造二叉树的时候,一定需要知道中序遍历,因为中序遍历决定了结点间的相对左右位置关系.所以,对一串有序的数组,我们可以来构建二叉有序数,并通过中序遍历,就可以得到这个有序的数组. 既然中序遍历可以通过根结点将序

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to ri

[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】145. Binary Tree Postorder Traversal

题目如下: 解题思路:凑数题+3,搞不懂为什么本题的难度是Hard,而[leetcode]590. N-ary Tree Postorder Traversal是Medium. 代码如下: # Definition for a binary tree node. # class TreeNode(object): # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solutio