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. CTF-域渗透--HTTP服务--命令注入1

    开门见山 1. 扫描靶机ip,发现PCS 192.168.31.210 2. 用nmap扫描开放服务和服务版本 3. 再扫描全部信息 4. 探测http服务的目录信息 5. 再用dirb扫描 6. 查 ...

  2. 给rar文件加个自定义头

    unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms ...

  3. Emoji表情符号兼容方案

    Emoji表情符号兼容方案 一 什么是Emoji    emoji就是表情符号:词义来自日语(えもじ,e-moji,moji在日语中的含义是字符) 表情符号现已普遍应用于手机短信和网络聊天软件. em ...

  4. 201709-2 公共钥匙盒 Java

    思路: 按上课开始时间排一下序,一个时刻一个时刻判断要不要还钥匙,要不要借钥匙 import java.util.ArrayList; import java.util.Collections; im ...

  5. JavaScript—面向对象 贪吃蛇_3 蛇对象

    蛇对象 function Snake(element) { this.width = 20 this.height = 20 //蛇身 位置 颜色 this.body = [ {x: 6, y: 4, ...

  6. java 单利模式设计原理,饿汉式懒汉式,开发中推荐饿汉式

    单例模式的设计:  1 //Single类进内存,对象还没有存在,只有调用了getInstance方法时,才建立对象. //对象是方法被调用时,才初始化,也叫做对象的延时加载.成为:懒汉式. //Si ...

  7. LINUX之ntp时间同步服务配置

    本篇将介绍LINUX之ntp服务配置,时钟同步服务器配置.这个在很多地方都会用到,保持各主机之前的时间保持一致,保证主机之间的心跳稳定. 三台主机都是centos7 192.168.1.110 mas ...

  8. dp--01背包--Charm Bracelet

    Charm Bracelet Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, sh ...

  9. PAT Basic 插⼊与归并(25) [two pointers]

    题目 根据维基百科的定义: 插⼊排序是迭代算法,逐⼀获得输⼊数据,逐步产⽣有序的输出序列.每步迭代中,算法从输⼊序列中取出⼀元素,将之插⼊有序序列中正确的位置.如此迭代直到全部元素有序.归并排序进⾏如 ...

  10. Linux中的错误重定向你真的懂吗

    在很多定时任务里.shell里我们往往能看到 "2>&1",却不知道这背后的原理. 举个例子: * 1 * * * test.sh > /dev/null 2& ...