[LeetCode156] Binary Tree Upside Down
2015-08-13 13:22
459 查看
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
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
public TreeNode UpsideDownBinaryTree(TreeNode root) { if (root == null) return null; TreeNode parent = root, left = root.left, right = root.right; if (left != null) { TreeNode ret = UpsideDownBinaryTree(left); left.left = right; left.right = parent; return ret; } return root; }
相关文章推荐
- maven jetty
- Android如何快速打出100个渠道apk
- [经典算法题]寻找数组中第K大的数的方法总结
- 数据恢复陶工西数500GWD5000AAKX-08U6AA0摔敲盘不认紫色线圈磁头开盘恢复数据
- 缓存清理
- DOS命令之net use命令详细解释
- [前端] 音频/视频播放控制
- 11G新特性 -- 分区表和增量统计信息
- 网络编程复习_ Exp2
- Redmine的创建任务的性能问题
- 黑马程序员——java复习总结——多线程
- 正则表达式全部符号解释
- 中国获得2022年冬奥会举办权【经济学人】
- 解决Deprecated: mysql_connect(): The mysql extension is deprecated and will be removed in the future:
- 中国获得2022年冬奥会举办权【经济学人】
- Window_Open详解
- 工厂方法模式
- 数据恢复陶工SS6K21CB东芝USB3.016G优盘不识别无盘符主控损坏Flahs颗粒恢复数据
- Git常用操作 - 分支管理
- HierarchyViewer源代码理解(三)