Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
2024-08-29 21:59:54
题目:
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;
}
}
最新文章
- selenium配置
- C++注意事项
- viewgroup用addview添加的view不显示问题
- 利用manifest文件对程序目录下的dll进行分类
- 2016-08-05:samba服务器配置
- .Net性能优化时应该关注的数据
- MySQL Order By Rand()效率
- ios 加载本地html css文件 ps:css和html必须在同一文件夹下面
- struts2增删改查---layer---iframe层
- mysql 系统性浅聊 myisam 存储引擎【原创】
- Neutron:Firewall as a Service(FWaaS)
- frame buffer简单应用
- G面经Prepare: Print Zigzag Matrix
- C++学习笔记50:队列类模板
- API 'variant.getJavaCompiler()' is obsolete and has been replaced with 'variant.getJavaCompileProvider()'
- 【leetcode】350. Intersection of Two Arrays II
- 遍历DOM树,each()遍历
- sprintf拼接字符串的问题
- os模块和sys模块,以及random模块
- C# winform 请求http ( get , post 两种方式 )
热门文章
- Java常用工具类之IO流工具类
- 【伪暴力+智商剪枝】Codeforces Round #489 (Div. 2) D
- set集合玩法、三目运算
- 【BZOJ 1018】线段树 **
- Tomcat+Apache集群方案
- NOIP2017 D2T2宝藏
- MySQL注射绕过技巧(三)
- sqlserver -- 学习笔记(二)“SQL Server 阻止了对组件 'xp_cmdshell' 的 过程'sys.xp_cmdshell' 的访问”解决方法
- SCOJ 4484 The Graver Robbers' Chronicles 后缀自动机
- AS3使用Json 传复杂数据 -------- 用数组而不是向量