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 ...
随机推荐
- python 安装第三方库,超时报错--Read timed out.
Traceback (most recent call last): File "/home/xiaoduc/.pyenv/versions/3.5.0/lib/python3.5/site ...
- TCP/IP 学习博客
原作者地址:http://blog.csdn.net/goodboy1881/article/category/204448
- iOS项目里面如何清理缓存
在正式讲解以前,请先看一下以下图片,在以下这款APP种设有清理缓存,开始我以为很复杂,在弄明白之后,其实就是几句代码就解决了. 在实际项目开发中,我们很多的文件都会缓存在沙盒里面,比如:照片 ...
- jQuery 源码分析4: jQuery.extend
jQuery.extend是jQuery最重要的方法之一,下面看看jQuery是怎样实现扩展操作的 // 如果传入一个对象,这个对象的属性会被添加到jQuery对象中 // 如果传入两个或多个对象,所 ...
- jquery 在页面中按回车 响应 事件
为了用户方便我们往往会在用户回车之后做一些事,比如登陆的时候,填完表单过后,我们习惯性的会直接按回车,当然要处理这个,jquery是很简单的,我们来看看怎么做吧. $(document).ready( ...
- Date、String、Calendar类型之间的转化
原文出处:http://fjfj910.iteye.com/blog/1202219 1.Calendar 转化 String //获取当前时间的具体情况,如年,月,日,week,date,分,秒等 ...
- javascript学习笔记4
1. 分析一下代码执行结果 分析为什么? var a = 12; b = 34; c = 56; ++a; //a结果 13 a++; //a结果 14 c = ++a + b; ...
- yii YII小部件 创建登录表单表单 Login表单
YII框架必须遵循其表单的创建方法 登录模型错做与数据库操作模型是一致的,不同的是不跟数据库交互 ,用的是小部件,在创建表单之前,要在用户控制模块完成以下代码 protected --models - ...
- laravel homestead
laravel homestead真是个好东西啊,折腾了很长时间,终于ok啦. 安装成功之后,在-目录下有个homstead,进入执行vagrant up clzdeMBP:Homestead clz ...
- Win2008 R2 IIS7.5+PHP5(FastCGI)+MySQL5环境搭建教程
现在很多朋友想尝试win2008 r2来跑web服务器,跟win2003相比界面差别有点大,有些人可能不太习惯,不过以后是趋势啊,这里简单分享下,方便需要的朋友 准备篇 一.环境说明: 操作系统:Wi ...