码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode#156] Binary Tree Upside Down

时间:2015-09-15 06:56:07      阅读:147      评论:0      收藏:0      [点我收藏+]

标签:

Problem:

Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that shares the same parent node) or empty, flip it upside down and turn it into a tree where the original right nodes turned into left leaf nodes. Return the new root.

For example:
Given a binary tree {1,2,3,4,5},

    1
   /   2   3
 / 4   5

 

return the root of the binary tree [4,5,2,#,#,3,1].

   4
  /  5   2
    /    3   1  

 

confused what "{1,#,2,3}" means? > read more on how binary tree is serialized on OJ.

Wrong Solution:

public class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if (root == null)
            return root;
        TreeNode upsided_left = upsideDownBinaryTree(root.left);
        TreeNode upsided_right = upsideDownBinaryTree(root.right);
        if (upsided_left == null)
            return root;
        root.left = null;
        root.right = null;
        TreeNode right_most = upsided_left;
        if (right_most.right != null) 
            right_most = right_most.right;
        right_most.left = upsided_right;
        right_most.right = root;
        return upsided_left;
    }
}

Mistakes Analysis:

Mistake analysis:
Input:
[1,2,null,3,null,4]
Output:
[4,null,3,null,1]
Expected:
[4,null,3,null,2,null,1]

The above code is complicated and wrong. Cause I don‘t throughly understand the logic behind this problem. 
I even try to find the right insert position at the upsided result, which is a indicator of wrong. 

Actually the idea is really not hard.
For a tree, we must sure,
1. right child must be a leaf node or empty.
2. the current left subtree would become the new root (see it as a single node).

I understand the above two important points.
But I miss this one.
3. iff the left subtree is not a single node (the root of the left sub-tree would become its upsidedtree‘s rightmost node, where we should attach the root as left child and root as right child)
Very important!!!!

Thus above solution is complex and wrong.
fix 1:  TreeNode upsided_right = upsideDownBinaryTree(root.right);
Since right child must be a leaf or empty, there is no need to perform upsideDownBinaryTree over it.

fix 2: Don‘t try to search to rightmost node manually, the left child of current tree has already been turned into the right most node.
TreeNode right_most = upsided_left;
if (right_most.right != null) 
    right_most = right_most.right;
right_most.left = upsided_right;
right_most.right = root;

root pointer has not been updated after "upsideDownBinaryTree(root.left)", root.left still point to it‘s left child before upsided. (root‘s informaiton has not been changed yet!!!)
root.left.left = root.right;
root.left.right = root;

You should also pay attention to the base case of this solution.
It could be null pointer or leaf node
null pointer : root == null
leaf node : root.left == null && root.right == null
In case we need to continue to search at next level when this only left child.

Solution:

public class Solution {
    public TreeNode upsideDownBinaryTree(TreeNode root) {
        if (root == null || root.left == null && root.right == null)
            return root;
        TreeNode newRoot = upsideDownBinaryTree(root.left);
        root.left.left = root.right;
        root.left.right = root;

        root.left = null;
        root.right = null;

        return newRoot;
    }
}

 

[LeetCode#156] Binary Tree Upside Down

标签:

原文地址:http://www.cnblogs.com/airwindow/p/4809040.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!