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. python期末考试复习

    期末考试复习 补修的python跟着大一一起学,考试肯定不会出难,于是就敲了一些代码,把他们放到博客上,来记录一下 代码都是一段一段的,且python代码不是很多,所以我都写到了一个文件里,作为练习 ...

  2. JKS not Found

    近期使用Spring Boot开发微信验证的时候, 在获取token时,Idea老是提示Jks not found,网上找资料,都说是SSL的问题 实际解决方法: 重装JDK,将JDK重装之后,运行正 ...

  3. 【mac相关bash文件】

    mac 下 关于 .bashrc 和 .bash_profile 1.首先.bashrc 可能自带的系统里没有这个文件. 2.bash_profile  里边一半放的是PATH相关. 3. .bash ...

  4. java链接redis服务器

    1.首先你需要下载驱动包jedis.jar确保下载最新驱动包. 2.public class RedisUtil { //服务器IP地址 private static String ADDR = &q ...

  5. css 元素选择器

    子元素选择器 h1 > strong {color:red;} //这个规则会把第一个 h1 下面的两个 strong 元素变为红色,但是第二个 h1 中的 strong 不受影响: <h ...

  6. Qt5学习笔记(1)-环境配置(win+64bit+VS2013)

    Qt5学习笔记(1)-环境配置 工欲善其事必先-不装-所以装软件 久不露面,赶紧打下酱油. 下载 地址:http://download.qt.io/ 这个小网页就可以下载到跟Qt有关的几乎所有大部分东 ...

  7. 微信官方小程序示例demo 微信开发者工具打开不显示云开发按钮

    如果直接打开官方的demo,微信开发者工具上是不显示云开发按钮的. 是因为默认appid是测试号.要换成一个正式appid就会显示云开发按钮了. 分享一个朋友的人工智能教程.零基础!通俗易懂!风趣幽默 ...

  8. Scanner方式输入小写字母转换成大写字母

    import java.util.Scanner; /**  * 小写字母转换成大写字母      * @author zzu119  *  */ public class letterTransfe ...

  9. Kattis dragonball1 Dragon Ball I(最短路)

    There is a legendary tale about Dragon Balls on Planet X: if one collects seven Dragon Balls, the Dr ...

  10. Web 全栈大会:万维网之父的数据主权革命

    大家好,今天我和大家分享一下由万维网之父发起的一场数据主权革命.什么叫数据主权?很容易理解,现在我们的数据是把持在巨头手里的,你的微信通讯录和聊天记录都无法导出,不管是从人权角度还是从法理角度,这些数 ...