[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 ...
随机推荐
- C#微信公众号开发 -- (四)获取API调用所需的全局唯一票据access_token
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token.开发者需要进行妥善保存.access_token的存储至少要保留512个字符空间.access_to ...
- WORDPRESS 后台500错误解决方法集合
引自: http://www.guuglc.com/565.html 这篇文章本质上我是不可能会写到,就因为7号那天晚上,我准备搬家的时候,发现前台完好,进入后台却500错误. 这时我就得急的,毕竟明 ...
- ios NSMethodSignature and NSInvocation 消息转发
1.首先获取消息转发时连个函数内部具体内容 MARK:这里是拿[@"xxxxx" length]调用拿来举例说明 (lldb) po signature <NSMethodS ...
- c常用字符串函数
获取字符串长度 : size_t strlen(const char *str); 字符串拷贝函数: 把src中内容拷贝到dest中,它会覆盖原来的内容,它会把src中的\0,没有覆盖内容不变 如果s ...
- Spring中Bean实例的生命周期及其行为
- HOW TO: Creating your MSI installer using Microsoft Visual Studio* 2008
Quote from: http://software.intel.com/en-us/articles/how-to-creating-your-msi-installer-using-visual ...
- HttpClient4.3 使用经验(一) 简单使用
package com.wp.nevel.base.utils; import java.io.BufferedOutputStream; import java.io.BufferedReader; ...
- 访问Access数据库(有多个数据库时 体现多态)
如果想编写单机版MIS.小型网站等对数据库性能要求不高的系统,又不想安装SQLServer,可以使用Access(MDAC),只要一个mdb文件就可以了.使用Access创建mdb文件,建表.OleD ...
- ArrayList 转换为DataTable
, }; ArrayList list = new ArrayList(arr); var dt = new DataTable(); DataColumn dc = new DataColumn(& ...
- js 不可变的原始值和可变的对象引用
javascript中的原始值(undefined.null.布尔值.数字和字符串)与对象(包括数组和函数)有着根本区别.原始值是不可更改的:任何方法都无法更改(或“突变”)一个原始值.对数字和布尔值 ...