原题链接在这里: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. 1
  2. / \
  3. 2 3
  4. / \
  5. 4 5

return the root of the binary tree [4,5,2,#,#,3,1].

  1. 4
  2. / \
  3. 5 2
  4. / \
  5. 3 1

题解:

Recursion 方法是自底向上.

Time Complexity: O(n).

Space: O(n). tree height.

AC Java:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public TreeNode upsideDownBinaryTree(TreeNode root) {
  12. if(root == null || root.left == null){
  13. return root;
  14. }
  15. TreeNode newRoot = upsideDownBinaryTree(root.left);
  16.  
  17. root.left.left = root.right;
  18. root.left.right = root;
  19.  
  20. root.left = null;
  21. root.right = null;
  22. return newRoot;
  23. }
  24. }

Iterative 是从上到下.

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

AC Java:

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution {
  11. public TreeNode upsideDownBinaryTree(TreeNode root) {
  12. if(root == null || root.left == null){
  13. return root;
  14. }
  15. TreeNode cur = root;
  16. TreeNode next = null;
  17. TreeNode pre = null;
  18. TreeNode temp = null;
  19. while(cur != null){
  20. next = cur.left;
  21. cur.left = temp;
  22. temp = cur.right;
  23. cur.right = pre;
  24. pre = cur;
  25. cur = next;
  26. }
  27. return pre;
  28. }
  29. }

类似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. Mac or Centos 下如何编译objective-C

    #import <Foundation/Foundation.h> int main(int argc,const char *argv[]){ @autoreleasepool{ NSL ...

  2. (转载)c# winform 用子窗体刷新父窗体,子窗体改变父窗体控件的值

    第一种方法: 用委托,Form2和Form3是同一组 Form2 C#代码 using System; using System.Collections.Generic; using System.C ...

  3. 新鲜出炉的30个精美的 jQuery & CSS3 效果【附演示和教程】

    新鲜出炉的30个精美的 jQuery & CSS3 效果[附演示和教程]   作为最流行的 JavaScript 开发框架,jQuery 在现在的 Web 开发项目中扮演着重要角色,它简化了 ...

  4. 3. Configure the Identity Service

    Controller Node: 安装认证服务: 1. sudo apt-get install keystone   2. sudo vi /etc/keystone/keystone.conf [ ...

  5. 获取某个Group中所有对象的DisplayName

    $SANs = Get-ADGroupMember -Identity "CN=gAPCHN-HGZ-IE10-Users,OU=Groups,OU=Hangzhou - China,OU= ...

  6. eclipse远程调试

    -Xdebug -Xrunjdwp:transport=dt_socket,address=127.0.0.1:8000Eclipse 菜单上的 Window > Preferences > ...

  7. CSS3系列:流式(弹性)布局(flex布局)

    我的新伸缩盒子.http://www.cnblogs.com/leee/p/5533436.html

  8. 【ArcGis for javascript从零开始】之一 ArcGis加载天地图

    最近做项目需要用到ArcGis来进行数据展示和数据分析.以前从来没有接触过与Gis有关的东西,一切需要从头开始学.没有时间从头系统地学习了,只能用到哪个学习哪里了,本系列只是对学习的路径进行记录.Ar ...

  9. ptmalloc2源码解析初探

    本文是徽沪一郞在学习华庭(庄明强)所撰<glibc内存管理-ptmalloc2源代码分析>的阅读笔记.本笔记以slides的方式加以呈现.文件采用latex+tikz编辑而成,如果对lat ...

  10. Mininet实验 基于Mininet实现BGP路径挟持攻击实验

    参考:基于Mininet实现BGP路径挟持攻击实验 实验目的: 掌握如何mininet内模拟AS. 掌握BGP路径挟持的原理和分析过程. 实验原理: 互联网是由相互连接的自治系统AS组成的,通过一个通 ...