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. Centos7开机启动自己的脚本

    1. 自己新建一个脚本,如centnet-service.sh 经过后面的几个步骤后,这个脚本在开机的时候会执行,在这个脚本里面可以写你开机的时候想执行的命令,如启动tomcat,oracle等服务. ...

  2. 文献阅读报告 - Situation-Aware Pedestrian Trajectory Prediction with Spatio-Temporal Attention Model

    目录 概览 描述:模型基于LSTM神经网络提出新型的Spatio-Temporal Graph(时空图),旨在实现在拥挤的环境下,通过将行人-行人,行人-静态物品两类交互纳入考虑,对行人的轨迹做出预测 ...

  3. kaggle——贷款信用评估介绍

    介绍 对于金融机构的贷款业务来说,一个顾客的信用信息是极其重要的.因为只有了解客户的信用情况,才能决定是否通过客户的贷款申请.本次将会介绍如何根据用户的一些基本信息来判断顾客的信用或贷款偿还能力. 知 ...

  4. 如何写出高质量的Python代码--做好优化--改进算法点滴做起

    小伙伴你的程序还是停留在糊墙吗?优化代码可以显示程序员的素质欧! 普及一下基础了欧: 一层for简写:y = [1,2,3,4,5,6],[(i*2) for i in y ]       会输出  ...

  5. 简单vue的监听属性(单位换算)

    单位换算 单位换算可以根据实际情况换 <!DOCTYPE html> <html lang="en"> <head> <meta char ...

  6. 吴裕雄--天生自然MySQL学习笔记:MySQL 删除数据表

    MySQL中删除数据表是非常容易操作的, 但是在进行删除表操作时要非常小心,因为执行删除命令后所有数据都会消失. 语法 以下为删除MySQL数据表的通用语法: DROP TABLE table_nam ...

  7. 光纤卡网卡的区别以及HBA的常规定义-----引自百度百科

    在讨论这个问题的时候,需要先说清楚一个问题:我们知道,在早期的SAN存储系统中,服务器与交换机的数据传输是通过光纤进行的,因为服务器是把SCSI指令传输到存储设备上,不能走普通LAN网的IP协议,所以 ...

  8. http head详解

    Http普通报头:       少数报头域用于所有的请求和响应消息, 但并不用于被传输的实体 cache-Control: 用于指定缓存指令, 缓存指令是单向的 ,且是独立的(一个消息的缓存指令不会影 ...

  9. error: snap "electronic-wechat" has "install-snap" change in progress

    今天因为要使用 wechat ,但是因为 wechat 并没有官方的 Ubuntu 版本,幸好有大神出了 electronic-wechat ,可以直接在应用商店中搜到,然后直接安装,也可以命令行安装 ...

  10. Python语言学习:homework2

    三级菜单 # Author:Crystal data = { '北京':{ "昌平":{ "沙河":["oldbay","test ...