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 ...
随机推荐
- MySql 5.7密码查看或修改
一.启动命令行,输入: taskkill /f /im mysqld.exe //关闭mysql 二.转入mysql的bin目录下 三.输入:mysqld --skip-grant-tables // ...
- ###《Effective STL》--Chapter1
点击查看Evernote原文. #@author: gr #@date: 2014-09-12 #@email: forgerui@gmail.com Chapter1 容器 Topic 4: 调用e ...
- 暑假集训(3)第二弹 -----Jungle Roads(Hdu1301)
问题梗概:自从上次某个acmer来设计了拉格瑞圣岛的交通路线后,岛上的酋长就相当苦恼,他发现,虽然这些修好的公路便利了岛上的 交通,并且让拉格瑞圣岛的旅游业更加兴旺,甚至他们还收到了一笔不小的国际资金 ...
- vim 的 tags 模块 与 ctags
1. 概述 一般来说,在代码中跳转,离不开 ctags. 实际上,vim 中代码跳转是由 vim tags 模块完成的,tags 模块依赖于 tags 文件. ctags(Generate tag f ...
- 简单模拟Hibernate的主要功能实现
在学习期间接触到Hibernate框架,这是一款非常优秀的O/R映射框架,大大简化了在开发web项目过程中对数据库的操作.这里就简单模拟其底层的实现. /*******代码部分,及其主要注解***** ...
- 支付宝api教程,支付宝根据交易号自动充值
最近公司要用php做一个网站支付宝自动充值的功能,具体就是客户把钱直接转到公司的支付宝账号里,然后在我们网站上填写上交易号,我们网站程序自动获取交易信息,自动给网站的账户充值. 我的具体想法就是利用支 ...
- free -m
free -m total used free shared buffers cached Mem: 7760 1572 6187 0 9 ...
- JS判断上传图片格式是否正确
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- 设置nginx禁止通过IP访问服务器的方法
在Nginx上设置禁止通过IP访问服务器,只允许通过域名访问,以避免别人把未备案的域名解析到自己的服务器IP而导致服务器被断网. nginx的默认虚拟主机允许用户通过IP访问,或者通过未设置的域名访问 ...
- ubuntu漂亮主题
桌面看腻了?试试这 4 款漂亮的 Linux 图标主题吧 http://linux.cn/article-4332-1.html Flatabulous https://github.com/anmo ...