Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]
题目:
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;
}
}
Leet Code OJ 226. Invert Binary Tree [Difficulty: Easy]的更多相关文章
- LeetCode OJ 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- Leet Code OJ 219. Contains Duplicate II [Difficulty: Easy]
题目: Given an array of integers and an integer k, find out whether there are two distinct indices i a ...
- <LeetCode OJ> 226. Invert Binary Tree
226. Invert Binary Tree Total Accepted: 57653 Total Submissions: 136144 Difficulty: Easy Invert a bi ...
- C#版 - 226. Invert Binary Tree(剑指offer 面试题19) - 题解
版权声明: 本文为博主Bravo Yeung(知乎UserName同名)的原创文章,欲转载请先私信获博主允许,转载时请附上网址 http://blog.csdn.net/lzuacm. C#版 - 2 ...
- 226. Invert Binary Tree(C++)
226. Invert Binary Tree Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 ...
- LeetCode Javascript实现 258. Add Digits 104. Maximum Depth of Binary Tree 226. Invert Binary Tree
258. Add Digits Digit root 数根问题 /** * @param {number} num * @return {number} */ var addDigits = func ...
- Python解Leetcode: 226. Invert Binary Tree
leetcode 226. Invert Binary Tree 倒置二叉树 思路:分别倒置左边和右边的结点,然后把根结点的左右指针分别指向右左倒置后返回的根结点. # Definition for ...
- Leetcode 226. Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 class Solution(object): ...
- Java for LeetCode 226 Invert Binary Tree
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia: This problem wa ...
随机推荐
- Bzoj2002/洛谷P3203 [HNOI2010]弹飞绵羊(分块)
题面 Bzoj 洛谷 题解 大力分块,分块大小\(\sqrt n\),对于每一个元素记一下跳多少次能跳到下一个块,以及跳到下一个块的哪个位置,修改的时候时候只需要更新元素所在的那一块即可,然后询问也是 ...
- TCP的那些事-2
这篇文章是下篇,所以如果你对TCP不熟悉的话,还请你先看看上篇<TCP的那些事儿(上)> 上篇中,我们介绍了TCP的协议头.状态机.数据重传中的东西.但是TCP要解决一个很大的事,那就是要 ...
- openstack首页默认跳转
在/var/www/html新建index.html <html> <head> <meta http-equiv="Content-Language" ...
- 【BZOJ 3442】 3442: 学习小组 (最大费用流)
3442: 学习小组 Time Limit: 5 Sec Memory Limit: 128 MBSubmit: 403 Solved: 193 Description [背景] 坑校准备鼓励学生 ...
- 让你的chrome开发工具console支持jquery
首先执行以下代码: ;(function(d,s){d.body.appendChild(s=d.createElement('script')).src='http://code.jquery.co ...
- 【8.19校内测试】【背包】【卡特兰数】【数位dp】
早上随便搞搞t1t3就开始划水了,t2一看就是组合数学看着肚子疼...结果t1t3都a了??感天动地. 从小到大排序,从前到后枚举i,表示i是整个背包中不选的物品中代价最小的那个,即i不选,1到i-1 ...
- bzoj 2733: [HNOI2012]永无乡 -- 线段树
2733: [HNOI2012]永无乡 Time Limit: 10 Sec Memory Limit: 128 MB Description 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自 ...
- python开发_shelve_完整版_博主推荐
''' python中的shelve模块,可以提供一些简单的数据操作 他和python中的dbm很相似. 区别如下: 都是以键值对的形式保存数据,不过在shelve模块中, key必须为字符串,而值可 ...
- [转]Android学习:EditText的使用方法
EditText是在Android开发中经常被使用到的控件,主要用来获取用户的输入内容. 1.EditText常用属性 EditText继承自TextView,所以EditText也拥 ...
- mybatis源码分析(6)-----核心调度对象StatmentHandler
写在前面 通过上一偏文章,我们知道:mybatis 的插件开发,主要是集中在Executor(执行器),ParameterHandler(参数处理器),ResultSetHandler(结果集处理器) ...