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的更多相关文章

  1. ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java

    156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions   Total Accepted: ...

  2. [leetcode]156.Binary Tree Upside Down颠倒二叉树

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  3. [LeetCode] 156. Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  4. 【LeetCode】Binary Tree Upside Down

    Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...

  5. 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  6. [LeetCode] 152. Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

  7. 156. Binary Tree Upside Down

    题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...

  8. 156. Binary Tree Upside Down反转二叉树

    [抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...

  9. [LC] 156. Binary Tree Upside Down

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

随机推荐

  1. 【JAVA错误笔记】 - Unable add facets project AnnotationWebService CXF 2-x Web Services

    错误描述: 创建webservice接口服务时候提示: Unable add facets project AnnotationWebService CXF 2-x Web Services Unab ...

  2. 查看SQL server 2008 R2 的Service Package 版本号(同样适用于SQL Server 2005)

    在SQL Server 中新建一个查询(new Query),然后输入下面的SQL脚本,即可看到当前的数据库的Service Package (补丁包)的版本号 select serverproper ...

  3. android使用广播退出应用程序

    由于在(Widget或Service.BroadcastReceiver中)使用startActivity()方法启动activity时需使用FLAG_ACTIVITY_NEW_TASK flag,所 ...

  4. SomeThing of Memcache

    Memcache for .net 文章一: http://blog.csdn.net/dinglang_2009/article/details/6917794 不定时更新

  5. ECMA5.1中关于encodeURI,decodeURI 和encodeComponentURI,decodeComponentURI的区别

    The encodeURI and decodeURI functions are intended to work with complete URIs; theyassume that any r ...

  6. Andriod 中常见错误

    1.Open quote is expected for attribute "android:name" associated with an element type &quo ...

  7. 迷你版 smarty --模板引擎和解析

    http://blog.ipodmp.com/archives/php-write-a-mini-smarty-template-engine/ 迷你版Smarty模板引擎目录结构如下: ① 要开发一 ...

  8. mysqli和mysql和pdo查询

      mysql mysql_connect($db_host, $db_user, $db_password); mysql_select_db($dn_name); $result = mysql_ ...

  9. Yum安装Memcache

    rpm -qa | grep libevent       yum install libevent -y rpm -qa | grep memcached yum install memcached ...

  10. python学习--string

    1\string are immutable, which means you can't change an existing string. >>>greeting = 'Hel ...