[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 ...
随机推荐
- 怎么用js代码改变单选框的选中状态
今天突然有一个需求要用到,使用js代码改变单选框的选中状态.当时想也不想直接 function doGender(gender) { if (gender == "男") { ge ...
- maven是什么?(转自oracle官网)
Maven 是一个项目管理和构建自动化工具.但是对于我们程序员来说,我们最关心的是它的项目构建功能.所以这里我们介绍的就是怎样用 maven 来满足我们项目的日常需要.Maven 使用惯例优于配置的原 ...
- Android线程与异步消息处理机制
在程序开发时,对于一些比较耗时的操作,我们通常会为其开辟一个单独的线程来执行,这样可以尽可能的减少用户等待的时间.在Android中,默认情况下,所有的操作都是在主线程中进行的,这个主线程负责管理与U ...
- swing容器继承重绘问题解决
swing容器继承重绘问题解决 以JPanel为例,继承JPanel,想动态为器更换背景,这就涉及到重绘问题.一下是本人重写代码: package ui; import java.awt.Grap ...
- 一些简单的帮助类(2)-- JavaSctipt Array Linq
在日程工作中经常会遇到这样的问题 一个JS数组 我们要找出其中 一些符合要求的类容 又或者对数组里的类容求和求平均数之类的一般的做法是循环里面的类容做判断添加到一个新的集合里 var array = ...
- File的文件提取的小练习
package com.java.Dmeo1.www; import java.io.File;import java.util.LinkedList;import java.util.TreeSet ...
- javaee学习-JSP指令简介
JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分. 在JSP 2.0规范中共定义了三个指令: page指令 Inclu ...
- Codevs 4560 NOIP2015 D2T2 子串
> 4560 NOIP2015 D2T2 子串 时间限制: 1 s 空间限制: 128000 KB 题目等级:黄金 Gold 题目描述 Description 有两个仅包含小写英文字母的字符串A ...
- Andriod 中常见错误
1.Open quote is expected for attribute "android:name" associated with an element type &quo ...
- mac 下 sublime text 运行c++/c 不能使用scanf/cin
{ "cmd": ["g++", "${file}", "-o", "${file_path}/${file_ ...