题目:

Invert a binary tree.

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 null;
}
if(root.left!=null){
root.left=invertTree(root.left);
}
if(root.right!=null){
root.right=invertTree(root.right);
}
TreeNode temp=root.right;
root.right=root.left;
root.left=temp;
return root;
}
}

最新文章

  1. selenium配置
  2. C++注意事项
  3. viewgroup用addview添加的view不显示问题
  4. 利用manifest文件对程序目录下的dll进行分类
  5. 2016-08-05:samba服务器配置
  6. .Net性能优化时应该关注的数据
  7. MySQL Order By Rand()效率
  8. ios 加载本地html css文件 ps:css和html必须在同一文件夹下面
  9. struts2增删改查---layer---iframe层
  10. mysql 系统性浅聊 myisam 存储引擎【原创】
  11. Neutron:Firewall as a Service(FWaaS)
  12. frame buffer简单应用
  13. G面经Prepare: Print Zigzag Matrix
  14. C++学习笔记50:队列类模板
  15. API 'variant.getJavaCompiler()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'
  16. 【leetcode】350. Intersection of Two Arrays II
  17. 遍历DOM树,each()遍历
  18. sprintf拼接字符串的问题
  19. os模块和sys模块,以及random模块
  20. C# winform 请求http ( get , post 两种方式 )

热门文章

  1. Java常用工具类之IO流工具类
  2. 【伪暴力+智商剪枝】Codeforces Round #489 (Div. 2) D
  3. set集合玩法、三目运算
  4. 【BZOJ 1018】线段树 **
  5. Tomcat+Apache集群方案
  6. NOIP2017 D2T2宝藏
  7. MySQL注射绕过技巧(三)
  8. sqlserver -- 学习笔记(二)“SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问”解决方法
  9. SCOJ 4484 The Graver Robbers' Chronicles 后缀自动机
  10. AS3使用Json 传复杂数据 -------- 用数组而不是向量