Invert Binary Tree 二叉树反转
2015-12-26 21:48
232 查看
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 { TreeNode temp; public TreeNode invertTree(TreeNode root) { if(root!=null){ temp=root.left; root.left=root.right; root.right=temp; if(root.left!=null) invertTree(root.left); //注意递归哦 if(root.right!=null) invertTree(root.right); } return root;//涵盖了root==null的情况 } }
相关文章推荐
- 玩转UITableView
- RSA算法应用及证明
- sqlmap tamper绕过安全狗
- online_judge_1152
- 算法导论第三十一(31)章数论算法
- android studio 获取android app sha1
- 中国余数定理
- Hadoop-2.4.0安装和wordcount执行验证
- MySQL向redis同步数据
- java.lang.String源码分析(2)
- codevs2597-团伙-解题报告
- Makefile 中:= ?= += =的区别
- 试找出被9,8,7除时,余数分别为1,2,3的所有整数x。
- LeetCode --- 70. Climbing Stairs
- (ARM v7)信号量、互斥体代码追踪
- Cocos2D-html5 公布游戏js编译为jsc
- highcharts
- BestCoder Round #67-N*M bulbs(规律)
- 寻找技术联合创始人
- SDUT 3401 数据结构实验之排序四:寻找大富翁(堆排序)