My code:
import java.util.ArrayList;
/**
* 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;
ArrayList<Integer> nodeSet = new ArrayList<Integer>();
flatten(nodeSet, root);
TreeNode newRoot = root;
for (int i = 1; i < nodeSet.size(); i++) {
newRoot.left = null;
newRoot.right = new TreeNode(nodeSet.get(i));
newRoot = newRoot.right;
}
}
private void flatten(ArrayList<Integer> nodeSet, TreeNode root) {
nodeSet.add(root.val);
if (root.left != null)
flatten(nodeSet, root.left);
if (root.right != null)
flatten(nodeSet, root.right);
}
public static void main(String[] args) {
TreeNode n1 = new TreeNode(1);
TreeNode n2 = new TreeNode(2);
TreeNode n3 = new TreeNode(3);
TreeNode n4 = new TreeNode(4);
TreeNode n5 = new TreeNode(5);
n1.left = n2;
n2.left = n3;
n2.right = n4;
n3.left = n5;
Solution test = new Solution();
test.flatten(n1);
}
}
My test result:
这道题目也没什么好说的,先用 preorder 把数字取出来放在一个arraylist里面,
然后再一个个取出来形成他要的形状。。。
还是写的时候想的太简单了。这道题目要求是 in place.
相应的解法是,
public class Solution {
public void flatten(TreeNode root) {
if (root == null)
return;
flattenTree(root);
}
public TreeNode flattenTree(TreeNode root) {
if (root == null)
return null;
TreeNode leftTail = flattenTree(root.left);
TreeNode rightTail = flattenTree(root.right);
if (leftTail == null && rightTail == null)
return root;
else if (leftTail == null && rightTail != null)
return rightTail;
if (leftTail != null) {
TreeNode temp = root.right;
root.right = root.left;
root.left = null;
leftTail.right = temp;
}
if (rightTail != null)
return rightTail;
else
return leftTail;
}
}
还是inorder 的思想。
所以,inorder 的时候,我们可以先写一些草稿代码。假设我们已经拿到了我们想要的结构。
下来该怎么操作?
然后,如何返回?
这样子的思路,一般都可以把代码写出来的!
参考博客:
http://bangbingsyb.blogspot.com/2014/11/leetcode-flatten-binary-tree-to-linked.html
**
总结: pre order tree
**
Anyway, Good luck, Richardo!
My code:
/**
* 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;
}
helper(root);
}
private TreeNode helper(TreeNode root) {
if (root == null) {
return null;
}
TreeNode temp = helper(root.left);
if (temp == null) {
TreeNode right = helper(root.right);
return right == null ? root : right;
}
temp.right = root.right;
root.right = root.left;
root.left = null;
TreeNode right = helper(temp.right);
return right == null ? temp : right;
}
}
思路还是比较清晰的。就是拿recursion做。
然后发现答案里面竟然可以拿iteration做。。。
My code:
/**
* 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;
}
while (root != null) {
TreeNode curr = root.left;
if (curr != null) {
while (curr.right != null) {
curr = curr.right;
}
curr.right = root.right;
root.right = root.left;
root.left = null;
}
root = root.right;
}
}
}
感觉还是很巧妙地。
和recursion的主要区别在于,
recursion 是从底部开始flatten
而iteration是从顶部开始flatten
Anyway, Good luck, Richardo! -- 09/07/2016
My code:
/**
* 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);
flatten(root.right);
TreeNode temp = root.right;
root.right = root.left;
root.left = null;
while (root.right != null) {
root = root.right;
}
root.right = temp;
}
}
reference:
https://discuss.leetcode.com/topic/41032/my-straightforward-dfs-java-code
之前的做法太难理解了。这个很直观。记住。
iteration 的做法也很巧妙。
Anyway, Good luck, Richardo! -- 09/25/2016