原题链接在这里:https://leetcode.com/problems/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

题解:

Recursion 方法是自底向上.

Time Complexity: O(n).

Space: O(n). tree height.

AC Java:

 /**
* 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;
}
}

Iterative 是从上到下.

Time Complexity: O(n). Space: O(1).

AC Java:

 /**
* 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 cur = root;
TreeNode next = null;
TreeNode pre = null;
TreeNode temp = null;
while(cur != null){
next = cur.left;
cur.left = temp;
temp = cur.right;
cur.right = pre;
pre = cur;
cur = next;
}
return pre;
}
}

类似Reverse Linked List.

LeetCode Binary Tree Upside Down的更多相关文章

  1. [LeetCode] Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

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

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

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

  4. LeetCode:Binary Tree Level Order Traversal I II

    LeetCode:Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of ...

  5. LeetCode: Binary Tree Traversal

    LeetCode: Binary Tree Traversal 题目:树的先序和后序. 后序地址:https://oj.leetcode.com/problems/binary-tree-postor ...

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

  7. [LeetCode] 152. Binary Tree Upside Down 二叉树的上下颠倒

    Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...

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

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

随机推荐

  1. thinkphp框架中session常识

    在看别人代码时候,发现他,在tp框架中使用session没有些session_start();然后我去查看了手册初始化设置方法 无需手动调用,在App类的初始化工作结束后会自动调用,通常项目只需要配置 ...

  2. https资料

    1.HTTPS的七个误解   http://blog.httpwatch.com/2011/01/28/top-7-myths-about-https/ 中文 http://www.cnblogs.c ...

  3. 批量硬关联本地AD帐号与Office云端帐号

    世纪互联给的方案, 说只能一个一个做硬匹配, 把我吓尿了. 我整个简单的, 还能批量做. 1. 将本地域中所有用户的这两个属性导出. Get-ADUser -Filter * -SearchBase ...

  4. Objective-C、C++和swift 的运行效率比较

    自己做iOS开发,以后慢慢都要转swift,前段时间看到网上的一个帖子,说swift的运行效率奇低,觉得自己有必要验证一下. 我用了一个最简单的加法运算,从0加到10000000,看三种语言的时耗. ...

  5. javascript Math.pow 函数 详解

    语法 Math.pow(x,y) 定义和用法 pow() 方法可返回 x 的 y 次幂的值. 处理简单数学问题 6的4次方等于1296,记作:64=1296; 求值: Math.pow(6,4)=12 ...

  6. Bootstrap页面布局9 - BS列表

    列表: 无序列表(列表中项目内容没有固定的顺序), 有序列表(通常使用在一组有前后顺序的内容上), 描述列表(定义解释一组词汇) 无序列表: <ul> <li>Ueditor编 ...

  7. thinkphp的save方法失败

    如果用下面的方式更新数据时, $data['link_phone'] = I('post.link_phone'); $flag1 = $order->save ($data); $data一定 ...

  8. hdf第二周,每天加班,周末加班,周日健身

    ----------------------------------------------------------------------------- 受到挫折,写一些简单的练习,增加成就感 放大 ...

  9. Types of compression algorithms

    http://www.html5rocks.com/en/tutorials/speed/img-compression/ Types of compression algorithms There ...

  10. Windows 下 Nginx + PHP + Xdebug + PHPStorm 调试环境配置

    前期条件:安装好 Nginx.PHP.PHPStorm,使得可以正常访问 一.为 PHP 安装 Xdebug 到 Xdebug 的官网(http://xdebug.org/download.php)下 ...