✡ 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 ...
随机推荐
- lucas 定理学习
大致意思就是求组合数C(n , m) % p的值, p为一个偶数 可以将组合数的n 和 m都理解为 p 进制的表示 n = ak*p^k + a(k-1)*p^(k-1) + ... + a1*p ...
- C语言实例代码
绘制余弦曲线和直线 #include #include int main() { double y; int x,m,n,yy; for(yy=0;yy<=20;yy++) {y=0.1*yy; ...
- S5PV210之beep-bus模型 linux3.0.8驱动
目录: 一. bus-driver-device模型 二. 运行结果,及错误解决 三. 怎样利用以有的driver device驱动来写自已的beep-driver-device 驱动 四 ...
- Microsoft Visual Studio Ultimate 2013 with Update 3 CN+EN
官方90天试用版. Microsoft Visual Studio Ultimate 2013 with Update 3 - 简体中文DVD5 ISO image (SHA-1: 9A306631A ...
- SharePoint 2013 Nintex Workflow 工作流帮助(十三)
博客地址 http://blog.csdn.net/foxdave 工作流动作 35. Delegate Workflow Task(User interaction分组) 该操作将委托未处理的工作流 ...
- self.view 的不当操作造成死循环
如题,在创建ContentView的时候,例子如下 NSString *viewClassName = NSStringFromClass([self class]); viewClassName = ...
- 用C#感受MongoDB MapReduce之魅力 转
MapReduce这个名词随着hadoop的用户的增多,越来越被人关注.MapReduce可谓MongoDB之中的亮点,我也想深入了解MapReduce,加上MongoDB操作简单,所以就选择了它.M ...
- hdu1116 欧拉回路
//Accepted 248 KB 125 ms //欧拉回路 //以26个字母为定点,一个单词为从首字母到末尾字母的一条边 //下面就是有向图判断欧拉回路 //连通+节点入度和==出度和 或者 存在 ...
- 3、C#基础整理(语句概述)
语句 语句分为四个大类: * 分支语句:if,if... else,if ...else if... else,switch case * 循环语句:for,while,do while,foreac ...
- magento中比较好的博客
magento web-开发 http://www.magentofront-end.com/magentomuban/category/web-frontend 水水博客专栏 http:// ...