226.Invert Binary Tree
2015-10-08 18:57
369 查看
Invert a binary tree.
to
4 / \ 2 7 / \ / \ 1 3 6 9
to
4 / \ 7 2 / \ / \ 9 6 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 invertTree(TreeNode root) { if(root == null) return root; TreeNode transit = root.left; root.left = root.right; root.right = transit; if(root.left != null) root.left = invertTree(root.left); if(root.right != null) root.right = invertTree(root.right); return root; } }
相关文章推荐
- 新闻的频道订阅界面和功能
- OpenCv获取摄像头并显示摄像内容
- 笨得不够
- Word Pattern
- 10-5总结
- 第四周
- 智能车学习(二)—— GPIO学习
- 启动虚拟机时报错----不能为虚拟电脑 1008 打开一个新任务.
- bzoj 1046: [HAOI2007]上升序列
- 关于代码规范问题(启动活动的最佳写法)
- ecshop 获取某个商品的 所有订单信息 或者销量
- iOS效果 :(1) CALayer 图层阴影,边框,圆角,圆型图片的阴影
- bzoj 2427[HAOI2010] 软件安装
- superoj912 resistance
- 完成与执行任务
- 283.Move Zeroes
- Android基础入门教程——2.4.14 ViewPager的简单使用
- 100.Same Tree
- iOS NSString 字符串处理:截取字符串、匹配字符串、分隔字符串、拼接字符串、替换字符串
- [转][译][Android基础]Android Fragment之间的通信