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. 1
  2. / \
  3. 2 3
  4. / \
  5. 4 5

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

  1. 4
  2. / \
  3. 5 2
  4. / \
  5. 3 1

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

Wrong Solution:

  1. public class Solution {
  2. public TreeNode upsideDownBinaryTree(TreeNode root) {
  3. if (root == null)
  4. return root;
  5. TreeNode upsided_left = upsideDownBinaryTree(root.left);
  6. TreeNode upsided_right = upsideDownBinaryTree(root.right);
  7. if (upsided_left == null)
  8. return root;
  9. root.left = null;
  10. root.right = null;
  11. TreeNode right_most = upsided_left;
  12. if (right_most.right != null)
  13. right_most = right_most.right;
  14. right_most.left = upsided_right;
  15. right_most.right = root;
  16. return upsided_left;
  17. }
  18. }

Mistakes Analysis:

  1. Mistake analysis:
  2. Input:
  3. [1,2,null,3,null,4]
  4. Output:
  5. [4,null,3,null,1]
  6. Expected:
  7. [4,null,3,null,2,null,1]
  8.  
  9. The above code is complicated and wrong. Cause I don't throughly understand the logic behind this problem.
  10. I even try to find the right insert position at the upsided result, which is a indicator of wrong.
  11.  
  12. Actually the idea is really not hard.
  13. For a tree, we must sure,
  14. 1. right child must be a leaf node or empty.
  15. 2. the current left subtree would become the new root (see it as a single node).
  16.  
  17. I understand the above two important points.
  18. But I miss this one.
  19. 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)
  20. Very important!!!!
  21.  
  22. Thus above solution is complex and wrong.
  23. fix 1: TreeNode upsided_right = upsideDownBinaryTree(root.right);
  24. Since right child must be a leaf or empty, there is no need to perform upsideDownBinaryTree over it.
  25.  
  26. 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.
  27. TreeNode right_most = upsided_left;
  28. if (right_most.right != null)
  29. right_most = right_most.right;
  30. right_most.left = upsided_right;
  31. right_most.right = root;
  32.  
  33. 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!!!)
  34. root.left.left = root.right;
  35. 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:

  1. public class Solution {
  2. public TreeNode upsideDownBinaryTree(TreeNode root) {
  3. if (root == null || root.left == null && root.right == null)
  4. return root;
  5. TreeNode newRoot = upsideDownBinaryTree(root.left);
  6. root.left.left = root.right;
  7. root.left.right = root;
  8.  
  9. root.left = null;
  10. root.right = null;
  11.  
  12. return newRoot;
  13. }
  14. }

[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. 怎么用js代码改变单选框的选中状态

    今天突然有一个需求要用到,使用js代码改变单选框的选中状态.当时想也不想直接 function doGender(gender) { if (gender == "男") { ge ...

  2. maven是什么?(转自oracle官网)

    Maven 是一个项目管理和构建自动化工具.但是对于我们程序员来说,我们最关心的是它的项目构建功能.所以这里我们介绍的就是怎样用 maven 来满足我们项目的日常需要.Maven 使用惯例优于配置的原 ...

  3. Android线程与异步消息处理机制

    在程序开发时,对于一些比较耗时的操作,我们通常会为其开辟一个单独的线程来执行,这样可以尽可能的减少用户等待的时间.在Android中,默认情况下,所有的操作都是在主线程中进行的,这个主线程负责管理与U ...

  4. swing容器继承重绘问题解决

    swing容器继承重绘问题解决   以JPanel为例,继承JPanel,想动态为器更换背景,这就涉及到重绘问题.一下是本人重写代码: package ui; import java.awt.Grap ...

  5. 一些简单的帮助类(2)-- JavaSctipt Array Linq

    在日程工作中经常会遇到这样的问题 一个JS数组 我们要找出其中 一些符合要求的类容 又或者对数组里的类容求和求平均数之类的一般的做法是循环里面的类容做判断添加到一个新的集合里 var array = ...

  6. File的文件提取的小练习

    package com.java.Dmeo1.www; import java.io.File;import java.util.LinkedList;import java.util.TreeSet ...

  7. javaee学习-JSP指令简介

    JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: page指令 Inclu ...

  8. Codevs 4560 NOIP2015 D2T2 子串

    > 4560 NOIP2015 D2T2 子串 时间限制: 1 s 空间限制: 128000 KB 题目等级:黄金 Gold 题目描述 Description 有两个仅包含小写英文字母的字符串A ...

  9. Andriod 中常见错误

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

  10. mac 下 sublime text 运行c++/c 不能使用scanf/cin

    { "cmd": ["g++", "${file}", "-o", "${file_path}/${file_ ...