Flatten Binary Tree to Linked List leetcode java
2014-07-24 00:08
387 查看
题目:
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
The flattened tree should look like:
2 public void flatten(TreeNode root) {
3 if(root == null)
4 return;
5
6 TreeNode realright = root.right;
7 if(lastvisited != null){
8 lastvisited.left = null;
9 lastvisited.right = root;
}
lastvisited = root;
flatten(root.left);
flatten(realright);
}
2 Stack<TreeNode> stack = new Stack<TreeNode>();
3 TreeNode p = root;
4
5 while(p != null || !stack.empty()){
6
7 if(p.right != null){
8 stack.push(p.right);
9 }
if(p.left != null){
p.right = p.left;
p.left = null;
}else if(!stack.empty()){
TreeNode temp = stack.pop();
p.right=temp;
}
p = p.right;
}
}
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 题解: 如hint所给出,这道题就是使用先序遍历,遍历到的值作为新的右孩子存起来,左孩子变为空。 注意的是,因为右孩子会更新,所以为了递归右子树,要在更新之前提前保存右孩子。 整个程序需要维护一个全局变量,保存当前所遍历的节点。 代码如下:1 TreeNode lastvisited = null;
2 public void flatten(TreeNode root) {
3 if(root == null)
4 return;
5
6 TreeNode realright = root.right;
7 if(lastvisited != null){
8 lastvisited.left = null;
9 lastvisited.right = root;
}
lastvisited = root;
flatten(root.left);
flatten(realright);
}
Reference:http://blog.csdn.net/perfect8886/article/details/20000083 此题还有不用递归方法解决的方法,那就是使用栈。 对整棵树一直向右子树方向遍历。当遍历的节点有右孩子时,就将其入栈。有左孩子时,将其更新为当前节点的右孩子,左孩子置空。当左孩子为空时而栈不空时, 就弹出栈,作为右孩子。代码如下:1 public void flatten(TreeNode root) {
2 Stack<TreeNode> stack = new Stack<TreeNode>();
3 TreeNode p = root;
4
5 while(p != null || !stack.empty()){
6
7 if(p.right != null){
8 stack.push(p.right);
9 }
if(p.left != null){
p.right = p.left;
p.left = null;
}else if(!stack.empty()){
TreeNode temp = stack.pop();
p.right=temp;
}
p = p.right;
}
}
Reference: //http://www.programcreek.com/2013/01/leetcode-flatten-binary-tree-to-linked-list/
相关文章推荐
- [Leetcode][JAVA] Flatten Binary Tree to Linked List
- leetcode:Flatten Binary Tree to Linked List 【Java】
- Java for LeetCode 114 Flatten Binary Tree to Linked List
- [LeetCode][Java] Flatten Binary Tree to Linked List
- [Leetcode] Flatten Binary Tree to Linked List (Java)
- leetcode JAVA Flatten Binary Tree to Linked List 难度系数3 3.40
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
- Flatten Binary Tree to Linked List leetcode java
- LeetCode – Flatten Binary Tree to Linked List (Java)
- 【LeetCode-面试算法经典-Java实现】【114-Flatten Binary Tree to Linked List(二叉树转单链表)】
- [leetcode]Flatten Binary Tree to Linked List
- [面试真题] LeetCode:Flatten Binary Tree to Linked List
- [Leetcode]Flatten Binary Tree to Linked List
- [LeetCode] Flatten Binary Tree to Linked List
- LeetCode Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- [leetcode] Flatten Binary Tree to Linked List
- Leetcode: Flatten Binary Tree to Linked List
- leetcode: Flatten Binary Tree to Linked List
- leetcode--Flatten Binary Tree to Linked List