[LC] 156. Binary Tree Upside Down
2024-10-08 20:00:42
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;
}
}
最新文章
- windows 物理内存获取
- Python之路第一课Day5--随堂笔记(模块)
- 百度地图用ip获取当前位置的经纬度(高精度)
- iOS_UIImage_图片旋转
- 你真的了解UIViewController跳转吗?
- unix exec族函数 关于参数的疑惑
- Android中获取IMSI和IMEI
- Stringbuffer与Stringbuilder源码学习和对比
- Javascript中函数的四种调用方式
- js的2种继承方式详解
- Dos下查询关闭端口的命令例子
- css笔记16:盒子模型的入门案例
- linux上配置jdk+Apache
- Centos6 编译安装局域网NTP服务器
- iOS开发之git学习
- 操作系统栈溢出检測之ucosII篇
- TEXT文本编辑框3 点击按钮添加文本至文本输入框
- main函数的两个参数
- BOM(浏览器对象模型)
- I used to know
热门文章
- MySQL--InnoDB 关键特性
- 程序Dog的大梦想
- 关于Java编码规范
- CentOS7设置阿里镜像教程
- delphi数据类型列表
- LA 6621 /ZOJ 3736 Pocket Cube 打表+暴力
- 17.3.12---socket
- ssh登录脚本
- 常用DOS命令(1) color,dir,copy,shutdown,mkdir(md),rmdir(rd),attrib,cd
- 2.node。框架express