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. ListView 实现多选/单选

    http://blog.csdn.net/ljfbest/article/details/40685327 ListView自身带了单选.多选模式,可通过listview.setChoiceMode来 ...

  2. android中实现“再按一次退出”功能

    首先,定义两次点击退出按钮的时间间隔:private static final long INTERNAL_TIME=2000; 然后,定义一个当前时间的变量:private long exitTim ...

  3. Solr4.8.1与Tomcat7整合

    Solr4.8.1和Tomcat7都可以到官方网站去下载,我这里就不多说了,如下图. 这里我们首先解压Solr-4.8.1.zip,再解压Tomcat,解压后,再在当前文件夹下建2个文件夹,一个用来放 ...

  4. Spring的多配置文件加载

    如果配置文件存在多个的情况下,加载配置文件的方式是:1--可以指定总的配置文件去包含子的配置文件,然后只加载总的配置文件即可在总配置文件applicationContext.xml 中引入子文件 &l ...

  5. C++ 变量转换

    atoi,atol,strtod,strtol,strtoul实现类型转换2006-02-13 版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明http://ivanvic.blogb ...

  6. 类库探源——System.Drawing.Bitmap

    一.System.Drawing.Bitmap Bitmap 类: 封装GDI+ 位图,此位图由图形图像及其属性的像素数据组成.Bitmap 是用于处理由像素定义的图像的对象 命名空间: System ...

  7. 第3章 Struts2框架--1、Struts2环境搭建

    第3章 Struts2框架--1.Struts2环境搭建 搭建步骤: 1.从下载http://struts.apache.org 没找到Struts2.3.16版,就下载了2.3.29 2.拷贝后解压 ...

  8. 九度OJ 1201 二叉排序树

    题目地址:http://ac.jobdu.com/problem.php?pid=1201 题目描述: 输入一系列整数,建立二叉排序数,并进行前序,中序,后序遍历. 输入: 输入第一行包括一个整数n( ...

  9. HBuilder手机Iphone运行提示“未受信用的企业级开发者”

    HBuilder手机Iphone运行提示“未受信用的企业级开发者” 解决方法:设置-----通用------设备管理-----点击Digtial Heaven....---信任"Digtia ...

  10. 《APUE》第三章笔记(2)

    read函数 调用read函数从打开的文件中读数据. #include <unistd.h> ssize_t read(int filedes, void *buf, size_t nby ...