[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 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.
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
Confused what [4,5,2,#,#,3,1]
means? Read more below on how binary tree is serialized on OJ.
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1
/ \
2 3
/
4
\
5
The above binary tree is serialized as [1,2,3,#,#,4,#,#,5]
.
题意:
将一棵树,按照一种方式上下颠倒
Solution1: Tree + Recursion
Need save the tree information before changing the tree structure
Now, the basic idea is to go though from the original root (2), make root’s left child(4) as newParent whose left child will be the original root’s right child (5) and right child will be the original root (2). After that, you set new root to root.left (5), and process it in the same way.
代码:
class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if(root == null || root.left == null) return root;
TreeNode newNode = upsideDownBinaryTree(root.left); // newNode最后要返回,所以找到、存下来后,就不再动了
root.left.left = root.right;// 这里为何不是newNode.left = root.left ?
root.left.right = root;
root.left = null ;
root.right = null;
return newNode;
}
}
[leetcode]156.Binary Tree Upside Down颠倒二叉树的更多相关文章
- ✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
156. Binary Tree Upside Down Add to List QuestionEditorial Solution My Submissions Total Accepted: ...
- [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 ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- [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 ...
- LeetCode 145 Binary Tree Postorder Traversal(二叉树的兴许遍历)+(二叉树、迭代)
翻译 给定一个二叉树.返回其兴许遍历的节点的值. 比如: 给定二叉树为 {1. #, 2, 3} 1 \ 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你能够用迭代来完毕它吗? 原文 ...
- [LeetCode] 199. Binary Tree Right Side View 二叉树的右侧视图
Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...
- [LeetCode] 298. Binary Tree Longest Consecutive Sequence 二叉树最长连续序列
Given a binary tree, find the length of the longest consecutive sequence path. The path refers to an ...
- 【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 ...
- [LeetCode] 152. Binary Tree Upside Down 二叉树的上下颠倒
Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node that ...
随机推荐
- 生产redis client 链接报:ERR max number of clients reached 含义: 达到最大客户端数错误
1.通过netstat 命令查看TCP又11822个连接 (netstat命令是一个监控TCP/IP网络的非常有用的工具) 2.默认redis最大的连接数10000 ,但是此时无法连接redis客户 ...
- python实现Content-Type类型为application/x-www-form-urlencoded发送POST请求
周日晚上接到公司的电话需要通过新榜的接口拿下微博热搜数据,拿到接口文档看了下很简单的一个post请求,主要根据时间段来获取热搜数据. 在实际编码的过程中经常遇到header的Content-Type的 ...
- docker镜像制作 centos6 nginx1.15.6 with NGINX_UPSYNC_MODULE
首先我选择了在centos6里部署nginx的镜像,如果大家选择的是centos7,自己重新修改吧 这里的问题点有几个: 1,make的版本选择,因为我下载了最新的cmake,需要c++11编译 这玩 ...
- vs2010安装的一些问题
VS安装出现的问题一般如果出现了 基本就不会安装成功.问题出现的原因有:w7系统的版本,有些可能会安装失败,其次就是你卸载的时候不要把相应 的库及.net的库卸载 后面再安装就容易出错.这个是安装 ...
- System类学习笔记
最近在学习源码的过程中发现:很多深层次的代码都用到了一个类System类,所以决定对System类一探究竟 本文先对System类进行了剖析,然后对System类做了总结 一.首先对该类的中的所有字段 ...
- problem:浏览器如何区分html超文本和普通文本
运营同学问:后端返回的一串元素标签,我想在网页中显示的时候,将标签中的内容渲染出来,不希望直接显示标签. 回答:bootstrap加模版组织的网页,模版渲染的数据只能渲染字符串,不能转化富文本. 运营 ...
- 初识TypeScript
环境配置 1,全局安装node和npm 2,安装TypeScript包 npm install typescript -g tsc --version 编写第一个ts程序 1,初始化项目 新建文件夹d ...
- 使用docker查看jvm状态,在docker中使用jmap,jstat
Docker中查看JVM的信息: 1. 列出docker容器:docker ps 2. 标准输入和关联终端:docker exec -it 容器ID bash 3. 查找出j ...
- 一切为了解决隐私问题,绿洲实验室Ekiden协议介绍
绿洲实验室官网截图 下一代区块链平台的竞争已经悄然展开,每个月我们都能看到新成立的创业公司宣称,他们要采用区块链解决所有问题.大约80-90%的区块链项目,运行在像Ethereum这样的平台上. 创建 ...
- .net mvc 分页
1.分页实体类 public class PageDto { public int PageIndex { get; set; } public int PageSize { get; set; } ...