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.
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
分析:
对于树,递归总是是很好的实现方法,所以这题应该
/**
* 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) {
return root;
}
TreeNode newRoot = upsideDownBinaryTree(root.left); root.left.right = root;
root.left.left = root.right; root.left = null;
root.right = null; return newRoot;
}
}
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: ...
- [Locked] 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】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] 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
原题链接在这里:https://leetcode.com/problems/binary-tree-upside-down/ Given a binary tree where all the rig ...
- 156. Binary Tree Upside Down
题目: Given a binary tree where all the right nodes are either leaf nodes with a sibling (a left node ...
- [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 ...
- [Swift]LeetCode156.二叉树的上下颠倒 $ 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 ...
随机推荐
- EF批量插入 扩展
https://efbulkinsert.codeplex.com/ https://github.com/loresoft/EntityFramework.Extended
- linq group join
本篇介绍Linq的Group和Join操作,继续使用<Linq 学习(3) 语法结构>中介绍的数据源. GroupGroup是进行分组操作,同SQL中的Group By类似.原型如下: p ...
- CKEditor的使用方法
CKEditor的使用方法 2014-03-31 09:44 8649人阅读 评论(1) 收藏 举报 版权声明:本文为博主原创文章,未经博主允许不得转载. ckeditor 的官方网站是 http:/ ...
- Python之路【第七篇续】:进程、线程、协程
Socket Server模块 SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端.即:每个客户端请求连接到服务器时 ...
- OC-苹果官方文档
苹果官方文档 help>documentation>左边iOS>language>objective>the objective-c programming langua ...
- Web Pages - Efficient Paging Without The WebGrid
Web Pages - Efficient Paging Without The WebGrid If you want to display your data over a number of p ...
- Spring之FactoryBean
首先要分辨BeanFactory 与 FactoryBean的区别, 两个名字很像,所以容易搞混 BeanFactory: 以Factory结尾,表示它是一个工厂类,是用于管理Bean的一个工厂 Fa ...
- vim配色方案设置(更换vim配色方案)
vim配色后,我的 设定底色为黑色,字体为绿色,然后将文件夹设为洋红,默认的注释换为淡黄:其实有一种简单的方法,就是设定为系统配置好的配色方案:转载文章如下: ---------------- ( ...
- 使用jQuery的Scrollify插件实现鼠标滚轮或者手势滑动到页面下一节点部分
有时我们需要做一个单页面介绍产品特性,而单页面内容非常多且页面非常长,为了快速定位到产品特性节点,我们使用js侦听用户滚轮事件,当用户触发滚轮滑动或者使用手势触屏滑动时,即可定位到相应的节点.一款jQ ...
- PHP中获取内网用户MAC地址(WINDOWS/linux)的实现代码
做一个内网根据MAC地址自动登录的应用,在WINDOW 2003可以正常使用,函数如下 复制代码 代码如下: function ce_getmac() { if(PHP_OS == 'WINNT' ...