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 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
链接: http://leetcode.com/problems/binary-tree-upside-down/
题解:
翻转二叉树。可以有recursive以及iterative两种方法。 Recursive是自底向上构建,很巧妙。 Iterative写得有点绕,二刷要好好再写一下。对于每个节点,要先保存这个节点,然后再进行修改,弄清楚reference就好写很多。
Recursive, Time Complexity - O(n), Space Complexity - O(logn)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
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;
}
}
Iterative, Time Complexity - O(n), Space Complexity - O(1)
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || (root.left == null && root.right == null))
return root;
TreeNode newLeft = null, newRight = null, oldRight = root.right, newRoot = root.left;
TreeNode prev = new TreeNode(root.val); while(newRoot != null) {
newLeft = newRoot.left;
newRight = newRoot.right;
newRoot.left = oldRight;
newRoot.right = prev;
prev = newRoot;
newRoot = newLeft;
oldRight = newRight;
} return prev;
}
}
更简练的Iterative写法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || (root.left == null && root.right == null))
return root;
TreeNode newRoot = root, prev = null, left = null, right = null; while(newRoot != null) {
left = newRoot.left;
newRoot.left = right;
right = newRoot.right;
newRoot.right = prev;
prev = newRoot;
newRoot = left;
} return prev;
}
}
二刷:
Java:
Recursive:
我们要递归返回新的root,新的root是原二叉树最左端节点。sibling变为新树左节点,原root变为新树右节点。
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null || root.left == 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;
}
}
Iterative1:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null) return root;
TreeNode left = root.left, right = root.right, newLeft = null, newRight = null;
root.left = null;
root.right = null;
while (left != null) {
newLeft = left.left;
newRight = left.right;
left.left = right;
left.right = root;
root = left;
left = newLeft;
right = newRight;
}
return root;
}
}
Iterative2:
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null) return root;
TreeNode left = null, right = null, prev = null;
while (root != null) {
left = root.left;
root.left = right;
right = root.right;
root.right = prev;
prev = root;
root = left;
}
return prev;
}
}
Reference:
https://leetcode.com/discuss/44718/clean-java-solution
https://leetcode.com/discuss/67458/simple-java-recursive-method-use-one-auxiliary-node
https://leetcode.com/discuss/18410/easy-o-n-iteration-solution-java
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
Problem: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- [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 ...
- [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 ...
- 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- [Locked] 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】Binary Tree Upside Down
Binary Tree Upside Down Given a binary tree where all the right nodes are either leaf nodes with a s ...
随机推荐
- JavaScript学习笔记(5)——JavaScript语法之数据类型
JavaScript 拥有动态类型.这意味着相同的变量可用作不同的类型: var x // x 为 undefined var x = 6; // x 为数字 var x = "Bill&q ...
- asp.net MVC URL路由入门指南
asp.net MVC 的URL路由是一个非常强大的功能,而且有个优点:强大单不复杂.然而,目前我在网上看到的相关资料,却都仅仅提供一些示例,仅通过这些示例,初学者基本上不可能明白为什么要这么配置,更 ...
- How to Change Password Complexity Requirements in Windows XP
Original Link: http://www.ehow.com/how_4812793_password-complexity-requirements-windows-xp.html#ixzz ...
- 清橙OJ 1082 查找第K小元素 -- 快速排序
题目地址:http://oj.tsinsen.com/A1082 问题描述 给定一个大小为n的数组s和一个整数K,请找出数组中的第K小元素. 这是一个补充程序的试题,你需要完成一个函数: int fi ...
- mysql 乱码问题处理
出现乱码的问题,主要就是因为数据在被处理的过程中,出现了编码和解码不对应造成的.因此解决编码问题的方法也就是在通过让编码和解码的过程能够对应起来就OK了,大学的而是,记得经常搞这个问题,今天又碰到了m ...
- away3d打包到IOS锯齿问题解决办法
很多ios设备是高清屏,一个像素顶普通设备四个像素.从这点上也许可以入手解决. 先把<requestedDisplayResolution>high</requestedDispla ...
- spl_autoload_register array参数
spl_autoload_register (PHP 5 >= 5.1.2) spl_autoload_register — 注册给定的函数作为 __autoload 的实现 说明¶ bool ...
- MVC 弹出提示框
第一种弹框成功后要刷新界面 [HttpPost] public ActionResult Add(Maticsoft.Model.Project.ProjectMoneyPlan model) { m ...
- Oracle 插入数据
6个柜面交易 打印修改--050101 delete from tran_prints where tran_id = (select id from tran where code='050101' ...
- uCOS-II任务的挂起和恢复
函数描述 OSTaskSuspend() 功能描述:无条件挂起一个任务.调用此函数的任务也可以传递参数OS_PRIO_SELF,挂起调用任务本身.函数原型:INT8U OSTaskSuspend ( ...