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 ...
随机推荐
- ###STL学习--适配器
点击查看Evernote原文. #@author: gr #@date: 2014-08-24 #@email: forgerui@gmail.com STL中的适配器. ###stl学习 |--迭代 ...
- 再议Unity 3D
一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...
- c# 中模拟一个模式匹配及匹配值抽取
摘一段模式的说明, F#的: msdn是这么描述它的:“模式”是用于转换输入数据的规则.模式将在整个 F# 语言中使用,采用多种方式将数据与一个或多个逻辑结构进行比较.将数据分解为各个构成部分,或从数 ...
- 九度OJ 1283 第一个只出现一次的字符
题目地址:http://ac.jobdu.com/problem.php?pid=1283 题目描述: 在一个字符串(1<=字符串长度<=10000,全部由大写字母组成)中找到第一个只出现 ...
- OpenJudge/Poj 1004 Financial Management
1.链接地址: http://poj.org/problem?id=1004 http://bailian.openjudge.cn/practice/1004 2.题目: 总时间限制: 1000ms ...
- 安卓热更新之Nuwa实现步骤
安卓热更新之Nuwa实现步骤 最近热更新热修复的功能在安卓应用上越发火热,终于我的产品也提出了相应的需求. 经过两天的研究,搞定了这个功能,在这里还要多谢大神们的博客,大神们的原理分析很到位,不过对于 ...
- Linux查找软件的安装路径
软件安装的路径可能不止一个,可以使用whereis命令查看软件安装的所有路径,以mysql为例: whereis mysql 该命令会返回软件的所有安装路径: mysql: /usr/bin/mysq ...
- Android开发系列之ListView
上篇博客解决了Android客户端通过WebService与服务器端程序进行交互的问题,这篇博客重点关注两个问题,一个是Android应用程序如何与本机文件型数据库SQLite进行交互,另一问题则是如 ...
- mac下如何查看指定端口被谁占用并且杀死该进程
在本地部署 Web 应用时我有遇到过某网络端口已经被其他程序占用的情况,这时候就需要先退出占用该端口的进程,我们可以通过“终端”来实现结束占用某特定端口的进程 1.打开终端,使用如下命令: lsof ...
- 一些web编程能用到的小知识
1 信用卡验证算法-luhn算法.(in django/utils/checksums.py) 1.从卡号最后一位数字开始,逆向将奇数位(1.3.5等等)相加.2.将偶数位数字相加,但是这里有个麻烦. ...