Flatten Binary Tree to Linked List
来源:互联网 发布:最难搞定的星座知乎 编辑:程序博客网 时间:2024/05/12 04:40
Given a binary tree, flatten it to a linked list in-place.
For example,
Given
1 / \ 2 5 / \ \ 3 4 6The flattened tree should look like:
1 \ 2 \ 3 \ 4 \ 5 \ 6
看着个思路:http://www.jyuan92.com/blog/leetcode-flatten-binary-tree-to-linked-list/
思路:in-order travel. 画图,保存左右两边node,然后移动,然后travel到左边的最低段,然后把右边的接上。
注意:判断leftnode!=null,边界情况。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public void flatten(TreeNode root) { if(root == null) return; flatten(root.left); TreeNode leftnode = root.left; TreeNode rightnode = root.right; if(leftnode!=null){ root.left = null; root.right = leftnode; } TreeNode node = leftnode; while(node!=null && node.right!=null){ node = node.right; } if(node!=null){ node.right = rightnode; } flatten(rightnode); } }
iterative写法1: 不用stack,算法就是左边不为空的时候,用一个指针走到左孩子的最右边的node,然后跟cur.right连起来。然后把左孩子换到右边,左孩子置空。
当前节点往右边移动一位,继续循环即可。这题最主要的考点就是,撸到左孩子的最右边,再进行衔接。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public void flatten(TreeNode root) { if(root == null) return; TreeNode cur = root; while(cur!=null){ TreeNode leftnode = cur.left; TreeNode rightnode = cur.right; if(leftnode!=null){ cur.left = null; TreeNode p = leftnode; while(p!=null && p.right!=null){ p = p.right; } p.right = rightnode; cur.right = leftnode; } cur = cur.right; } } }
iterative 方法2:用stack来记录右边的node:
对整棵树一直向右子树方向遍历。当遍历的节点有右孩子时,就将其入栈。有左孩子时,将其更新为当前节点的右孩子,左孩子置空。当左孩子为空时而栈不空时,
就弹出栈,作为右孩子。代码如下:
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public void flatten(TreeNode root) { if(root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode cur = root; while(cur!=null || !stack.empty()){ if(cur.right!=null){ stack.push(cur.right); } if(cur.left!=null){ TreeNode leftnode = cur.left; cur.left = null; cur.right = leftnode; } else { if(!stack.empty()){ TreeNode temp = stack.pop(); cur.right = temp; } } cur = cur.right; } }}
0 0
- 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
- 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
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- LeetCode - Flatten Binary Tree to Linked List
- 【leetcode】Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- [LeetCode]Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- Flatten Binary Tree to Linked List
- 公司制定的Java代码规范
- windows station和desktop
- 插入排序
- Java/Android开发规范——变量和常量命名
- NYOJ 93
- Flatten Binary Tree to Linked List
- J2EE——JNDI
- Sunday,每周日,身体自检
- 智能指针auto_ptr的简单实现
- 黑马程序员_java高新技术之动态代理
- android之Intent
- 大周末的
- Posix信号量
- 自定义Set