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.

Example:

  1. Input: [1,2,3,4,5]
  2.  
  3. 1
  4. / \
  5. 2 3
  6. / \
  7. 4 5
  8.  
  9. Output: return the root of the binary tree [4,5,2,#,#,3,1]
  10.  
  11. 4
  12. / \
  13. 5 2
  14. / \
  15. 3 1
  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. class Solution {
  11. public TreeNode upsideDownBinaryTree(TreeNode root) {
  12. if (root == null || root.left == null) {
  13. return root;
  14. }
  15. TreeNode newNode = upsideDownBinaryTree(root.left);
  16. root.left.left = root.right;
  17. root.left.right = root;
  18. root.left = null;
  19. root.right = null;
  20. return newNode;
  21. }
  22. }

[LC] 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. 156. Binary Tree Upside Down反转二叉树

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

  3. 156. Binary Tree Upside Down

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

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

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

  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. Git 小课堂 002——别名

    昨天我们聊了聊 Git 的文件存储,今天我们聊聊 Git 的别名.不知道你是不是熟悉别名,如果你经常使用命令行做一些事情,有一些复杂的命令,或者是一些简单的操作,往往用一些别名方法就很方便很容易,下面 ...

  2. JavaScript 之 Function

    JavaScript function 语句定义和用法: function 语句用于声明一个函数. 函数声明后,我们可以在需要的时候调用. 在 JavaScript 中,函数是对象,函数也有属性和方法 ...

  3. JAVAEE 和项目开发(第一课:浏览器和服务器的交互模式和HTTP协议的概念和介绍)

    互联网的发展非常迅速,但是万变不离其宗.学习 web 开发,需要我们对互 联的交互机制有一定的了解.为了更好的理解并掌握 Servlet,在正式学习 Servlet之前需要对 web 开发中客户端和服 ...

  4. 十六、matplotlib统计图

    '''Matplotlib 是一个Python的 2D绘图库.通过 Matplotlib,开发者可以仅需要几行代码,便可以生成绘图,直方图,功率谱,条形图,错误图,散点图等.通过学习Matplotli ...

  5. windows下使用mysqlbinlog做数据恢复时出现mysqlbinlog: File 'D:\MariaDB' not found (Errcode: 2)

    出现如下这种情况是因为为找到bin-log日志,但为什么没有查到了??? 从图中可以看出系统只到到了D:\MariaDB但路径并没有查全,默认在windows下是以空格为分隔符的,所以他把D:\Mar ...

  6. PAT-树-DFS-BFS相关问题解决方案整理

    如何建树? 二叉树-建树-方式一 dfs使用root左右指针建立树节点关系,返回根节点root 二叉树-建树-方式二 dfs使用二维数组,int nds[n][2],如:nds[i][0]表示i节点的 ...

  7. MySQL--SHOW ENGINE INNODB STATUS

    ===================================== -- :: 0x7f305b965700 INNODB MONITOR OUTPUT =================== ...

  8. Maven - No plugin found for prefix 'tomcat7' in the current project

    问题发现: 在构建Maven项目的时候,出现了No plugin found for prefix 'tomcat7' in the current project的错误. 是需要在Maven的Pom ...

  9. 创建DateFrame的常用四种方式

    import pandas as pd %pylab 一.使用numpy创建 df = pd.DataFrame(np.arange(16).reshape((4,4)), index=list('a ...

  10. php速成_day2

    一.PHP中的多维数组 1.多维数组及其用途 多维数组用来存储多个元素,元素是一个数组的结构. 之前学习的数组,是一个一维数组. $person = array( 'name' => 'xiao ...