题目:

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的更多相关文章

  1. ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java

    156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions   Total Accepted: ...

  2. [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 ...

  3. 156. Binary Tree Upside Down反转二叉树

    [抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...

  4. [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 ...

  5. [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 ...

  6. [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 ...

  7. 【LeetCode】156. Binary Tree Upside Down 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...

  8. [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 ...

  9. 【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 ...

随机推荐

  1. nodejs-fs使用

    (1)读取文本文件时须添加上'encoding'才能输出可读的内容. 02.txt hello,world! nodejs_readfile.js var fs = require('fs'); fs ...

  2. Codevs 1218 疫情控制 2012年NOIP全国联赛提高组

    1218 疫情控制 2012年NOIP全国联赛提高组 时间限制: 2 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description H 国有 n 个城市,这 ...

  3. springmvc学习(二)——使用RequestMapper请求映射

    本次内容是@RequestMapping,后面会有实例代码 Spring MVC 使用 @RequestMapping 注解为控制器指定可以处理哪些 URL 请求在控制器的类定义及方法定义处都可标注@ ...

  4. sgu 103 Traffic Lights

    这道题难得不是算法,而是处理. 题意就是让你求最短路,只有当两个点在某一秒颜色相同时,这条边才可以通行,输入首先给你 起点和终点, 然后给你 点数和边数, 接下来 n 行 初始颜色,初始颜色持续时间, ...

  5. java 中hashcode和equals 总结

    一.概述            在Java中hashCode的实现总是伴随着equals,他们是紧密配合的,你要是自己设计了其中一个,就要设计另外一个.当然在多数情况下,这两个方法是不用我们考虑的,直 ...

  6. 省市选择(基于zepto.js)

    效果如下: <div class="clList overflow-h mt75"> <select class="pull-left cl-35 se ...

  7. “微信应用号对行业影响”之一,app开发速来围观

    昨天,微信张小龙的一个讲话刷爆朋友圈,除了4大价值观,最后顺便提到:要推出微信应用号! 其实,价值观也就说说听听,最后顺便提到的微信应用号,才是真正的巨型炸弹. 腾讯挟6亿高粘度用户之重,号令天下,阿 ...

  8. linux(centos)搭建svn

    1.yum install subversion 2.输入rpm -ql subversion查看安装位置 输入 svn --help可以查看svn的使用方法 3.创建svn版本库目录 mkdir - ...

  9. Nodejs初学者福音

    Nodejs+Express+MongoDb 搭建个人博客  001 我喜欢把任务或者工作分解成工作流来完成,如下,后面将会按照流程来详述,希望能为Nodejs初学者及推广Nodejs做出些努力. n ...

  10. javascript 通用loading动画效果

    由于项目中多处要给ajax提交的时候增加等待动画效果,所以就写了一个简单的通用js方法: 代码如下: /*ajax提交的延时等待效果*/ var AjaxLoding = new Object(); ...