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;
}
}

最新文章

  1. windows 物理内存获取
  2. Python之路第一课Day5--随堂笔记(模块)
  3. 百度地图用ip获取当前位置的经纬度(高精度)
  4. iOS_UIImage_图片旋转
  5. 你真的了解UIViewController跳转吗?
  6. unix exec族函数 关于参数的疑惑
  7. Android中获取IMSI和IMEI
  8. Stringbuffer与Stringbuilder源码学习和对比
  9. Javascript中函数的四种调用方式
  10. js的2种继承方式详解
  11. Dos下查询关闭端口的命令例子
  12. css笔记16:盒子模型的入门案例
  13. linux上配置jdk+Apache
  14. Centos6 编译安装局域网NTP服务器
  15. iOS开发之git学习
  16. 操作系统栈溢出检測之ucosII篇
  17. TEXT文本编辑框3 点击按钮添加文本至文本输入框
  18. main函数的两个参数
  19. BOM(浏览器对象模型)
  20. I used to know

热门文章

  1. MySQL--InnoDB 关键特性
  2. 程序Dog的大梦想
  3. 关于Java编码规范
  4. CentOS7设置阿里镜像教程
  5. delphi数据类型列表
  6. LA 6621 /ZOJ 3736 Pocket Cube 打表+暴力
  7. 17.3.12---socket
  8. ssh登录脚本
  9. 常用DOS命令(1) color,dir,copy,shutdown,mkdir(md),rmdir(rd),attrib,cd
  10. 2.node。框架express