[LeetCode#156] Binary Tree Upside Down
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的更多相关文章
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- [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 ...
- [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 ...
- 【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 ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- [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 ...
- 156. Binary Tree Upside Down
题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- [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 ...
随机推荐
- 【JAVA错误笔记】 - 【Could not open ServletContext resource [/WEB-INF/applicationContext.xml]解决方法】
错误描述: Could not open ServletContext resource [/WEB-INF/applicationContext.xml] 原因分析: 问题主要由于加载spring的 ...
- Asp.Net alert弹出提示信息的5种方法
1.ClientScript.RegisterStartupScript(GetType(),"message","<script>alert('第一种方式, ...
- Linux中tshark(wireshark)抓包工具使用方法详解
在Linux下,当我们需要抓取网络数据包分析时,通常是使用tcpdump抓取网络raw数据包存到一个文件,然后下载到本地使用wireshark界面网络分析工具进行网络包分析.最近才发现,原来wires ...
- spring定时任务的配置
定时任务配置分为三个步骤: 1.定义任务 2.任务执行策略配置 3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="testTaskJob&qu ...
- angularjs ios title不能修改的bug的解决方法
在app.js加入下面这句代码 就可以解决. function ($rootScope, $state, $stateParams, $log, httpService, apiUrl, cookie ...
- 利用switch语句计算特定的年份的月份共有几天。
//利用switch语句计算特定的年份的月份共有几天. let year =2015 let month =2 //先判断闰年中二月份的情况 ifmonth ==2 { if (year %400 = ...
- OpenCV(4)-图像掩码操作(卷积)--平滑处理
卷积定义 矩阵的掩码操作即对图像进行卷积.对图像卷积操作的意义为:邻近像素对(包括该像素自身)对新像素的影响:影响大小取决于卷积核对应位置值得大小. 例如:图像增强可以使用 \[ I(i,j)=5*I ...
- Dynamic Programming: From novice to advanced
作者:Dumitru 出处:http://community.topcoder.com/tc?module=Static&d1=tutorials&d2=dynProg An impo ...
- layerX && layerY
转载:https://developer.mozilla.org/en-US/docs/Web/API/UIEvent/layerX UIEvent.layerX 非标准 这个属性是非标准的属性,并且 ...
- LINUX开机启动过程
LINUX开机启动过程 启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息 ...