✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java
- Total Accepted: 18225
- Total Submissions: 43407
- Difficulty: Medium
- Contributors: Admin
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.
For example:
Given a binary tree {1,2,3,4,5}
,
1
/ \
2 3
/ \
4 5
return the root of the binary tree [4,5,2,#,#,3,1]
.
4
/ \
5 2
/ \
3 1
confused what "{1,#,2,3}"
means? > read more 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}"
.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public TreeNode upsideDownBinaryTree(TreeNode root) {
if (root == null || (root.left == null && root.right == null)){
return root;
}
TreeNode node = root;
TreeNode nodeLeft = root.left;
TreeNode nodeRight = root.right;
while (nodeLeft != null){
TreeNode nodeChange = node;
TreeNode nodeLeftChange = nodeLeft;
TreeNode nodeRightChange = nodeRight;
node = nodeLeft;
nodeLeft = node.left;
nodeRight = node.right;
nodeLeftChange.left = nodeRightChange;
nodeLeftChange.right = nodeChange;
}
root.left = null;
root.right = null;
return node;
}
}
✡ leetcode 156. Binary Tree Upside Down 旋转树 --------- java的更多相关文章
- [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 ...
- [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] 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 ...
- 【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】156. Binary Tree Upside Down 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址:https://leet ...
- [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 ...
- 156. Binary Tree Upside Down
题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...
- 156. Binary Tree Upside Down反转二叉树
[抄题]: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left nod ...
- [LC] 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 ...
随机推荐
- [vijos P1014] 旅行商简化版
昨天早上上课讲旅行商问题,有点难,这周抽空把3^n的算法码码看.不过这个简化版已经够折腾人了. 其一不看解析不知道这是双进程动态规划,不过我看的解析停留在f[i,j]表示第一个人走到i.第二个人走到j ...
- Matlab与C/C++联合编程之Matlab以MEX方式调用C/C++代码(四)
利用Matlab与VC++联合编程,既可在C语言程序中打开Matlab引擎,调用Matlab的ToolBox函数和作图函数,也可在Matlab中调用C代码生成的动态链接库文件,用以加快执行速度.缩短开 ...
- idea使用generator自动生成model、mapper、mapper.xml(转)
原文链接:http://www.mamicode.com/info-detail-445217.html TEP 0.在Intellij IDEA创建maven项目(本过程比较简单,略) STEP 1 ...
- hadoop 常见问题
1.Eclipse 读取hdfs文件错误: java.io.IO Exception : Could not obtain block: blk_194219614024901469_1100 fi ...
- Add project to working sets
最近换了个电脑,重新搭建了开发环境,但是在新建项目的过程中发现有Add project to working sets这一个选项,一开始也不明白是什么意思,百度了一下,不少网友说是把项目存到物理空间, ...
- 自然数n的分解
输入自然数n(n<100),输出所有和的形式.不能重复. 如:4=1+1+2:4=1+2+1;4=2+1+1 属于一种分解形式. 样例: 输入: 7 输出: 7=1+6 7=1+1+5 7=1+ ...
- Ogre学习笔记Basic Tutorial 前四课总结
转自:http://blog.csdn.net/yanonsoftware/article/details/1011195 OGRE Homepage:http://www.ogre3d.org/ ...
- NCBI原始数据下载by Aspera Connect
主要参考这篇文章: http://mp.weixin.qq.com/s?__biz=MzA5NjU5NjQ4MA==&mid=2651154488&idx=1&sn=e693a ...
- VS2010编译Qt5.4.0静态库
http://www.kavenblog.com/?p=375 1.Qt的跨平台十分优秀,但是在Windows上是还是会有许多问题,其中之一就是动态链接库的问题,Qt程序的发布必须带一个体积不小的DL ...
- Fractal Tree
尝试使用递归方式实现一棵简单的分形树,给出初始点的坐标,在此基础上根据坐标轴旋转的规则计算出子树干与根节点的坐标关系,依次递归画出左子树干和右子树干,并提供一个递归的深度用于控制画的子树的数目. 在二 ...