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:

Input: [1,2,3,4,5]

    1
/ \
2 3
/ \
4 5 Output: return the root of the binary tree [4,5,2,#,#,3,1] 4
/ \
5 2
/ \
3 1
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null || root.left == null) {
return root;
}
TreeNode newNode = upsideDownBinaryTree(root.left);
root.left.left = root.right;
root.left.right = root;
root.left = null;
root.right = null;
return newNode;
}
}

[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. 零基础程序员入门Linux系统 !如何快速恢复系统?

    新手在学习Linux系统的时候,难免会遇到命令输错,或系统出错的难题.那么如何快速解决呢?本文就先给你一个后悔药,让你快速备份并恢复Linux系统.本文将以Ubuntu为例,在这之前,你需要一台服务器 ...

  2. Unity3d游戏代码保护

    现在的游戏项目如果达到一定规模.项目比较创新方竞争对手.项目严重依赖客户端代码那么代码保护还是尽量做,如果不是也没必须瞎折腾. Unity常见代码保护机制: 1.重新编译mono,修改mono_ima ...

  3. 吴裕雄--天生自然MySQL学习笔记:MySQL 数据类型

    MySQL中定义数据字段的类型对你数据库的优化是非常重要的. MySQL支持多种类型,大致可以分为三类:数值.日期/时间和字符串(字符)类型. 数值类型 MySQL支持所有标准SQL数值数据类型. 这 ...

  4. h5-FileReader对象的使用

    <!--FileReader对象的使用--> <!--需要及时预览 及时:当用户选择完图片之后就立即进行预览处理--onchange事件 预览:通过文件读取对象的readAsData ...

  5. LINUX的ssh免密码配置

    本篇将介绍LINUX的ssh免密码配置. 本篇中的有三台主机: 192.168.1.110 master.com.cn  主机192.168.1.111 salver1.com.cn192.168.1 ...

  6. Log4Net 使用及组合公共类

    好记性不如烂笔头,这次是由衷的感受到了! log4net 是一个很好用的日志记录工具,引用入项目中,如何查看项目内部运行情况,如何快速定位异常信息,好的日志记录能帮很大的忙: log4net 很好用, ...

  7. 201412-2 Z字形扫描 Java

    思路: 观察输出可以发现,可以不用定义 "方向" ,看斜线,如果是第偶数条(0也是偶数),从左下到右上输出.如果是第奇数条,从右上到左下输出. import java.util.S ...

  8. 面向对象 part6 继承

    继承 js实现的是实现继承/也就是继承实际的方法 //主要依赖:原型链 //基本思路: 就是一个引用类型继承另一个引用类型的属性和方法 详细:构造函数,实例,原型之间的关系.每个构造函数都有一个原型对 ...

  9. windows通过Winscp传输文件给Vbox虚拟机

    1.VirtualBox->设置->网络->高级->端口转发->Rule 1 TCP 127.0.0.1 22 10.0.2.15(在CentOS中通过ip addr命令 ...

  10. cygwin下命令行下切换目录

    比我们正常切换目录多个挂载的文件夹 cygdrive