[leetcode 226] Invert Tree
1 题目:
Invert a binary tree.
4
/ \
2 7
/ \ / \
1 3 6 9
to
4
/ \
7 2
/ \ / \
9 6 3 1
2 思路:
这是因为谷歌面试xx而著名的题,拿来做做。想了一会,想出来了,虽然代码量很多。。主要考察递归。
3 代码:
public TreeNode invertTree(TreeNode root) {
if(root == null) return null;
if(root.left != null && root.right != null){
TreeNode temp = root.left;
temp.left = root.left.left;
temp.right = root.left.right;
root.left = root.right;
root.right = temp;
}
if(root.left != null && root.right == null){
root.right = root.left;
root.left = null;
}else if(root.left == null && root.right != null){
root.left = root.right;
root.right = null;
}
invertTree(root.left);
invertTree(root.right);
return root;
}
最近一直在搞iOS,原先没考虑编程没怎么考虑浅拷贝、深拷贝的问题。java上
TreeNode temp = root.left;
貌似就是深拷贝了。
唐巧的代码则精简很多:
public class Solution {
public TreeNode invertTree(TreeNode root) {
if (root == null) {
return null;
}
root.left = invertTree(root.left);
root.right = invertTree(root.right);
TreeNode tmp = root.left;
root.left = root.right;
root.right = tmp;
return root;
}
}
anyway,之前也没怎么做过二叉树的题,能做出来就不错了。
[leetcode 226] Invert Tree的更多相关文章
- 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 was ...
- 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 ...
- (easy)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 was ...
- Java [Leetcode 226]Invert Binary Tree
题目描述: Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 解题思路: 我只想说递归大法好. ...
- Leetcode 226 Invert Binary Tree python
题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...
- leetcode 226 Invert Binary Tree 翻转二叉树
大牛没有能做出来的题,我们要好好做一做 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Tri ...
- Leetcode 226. Invert Binary Tree(easy)
Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 Trivia:This problem was ...
- LeetCode 226 Invert Binary Tree 解题报告
题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...
随机推荐
- js进度条实现
1.先设置CSS样式(可自定义) /*#region 进度条 */ .progbar { background-color: #e1e1e1; width:auto; color: #222; hei ...
- iptables-qos-tcpcopy-tc-tcpdump
QOS: https://www.chiphell.com/thread-427876-1-1.html iptables指南: http://man.chinaunix.net/network/ip ...
- PIC32MZ tutorial -- Output Compare
Output Compare is a powerful feature of embedded world. The PIC32 Output Compare module compares the ...
- 小杨同学git使用记(适合使用过git但是不熟练的童鞋)
首先声明:这不是一篇git使用手册或者指南,如果要详细的git使用指南,下面是廖雪峰的git教程,可以系统学习廖雪峰的git教程,当然,如果你想马上以一种正确的方式使用git,那么接下来你很快就会学会 ...
- DEBUG STACK TRACE for PoolBackedDataSource.close().
我使用generator生成的代码,xml里面的内容没有覆盖重写,而是在下面直接追加,所以需要把以前的全部删除,然后在用generator生成.如果这个不能解决问题,就查查别人的问题.
- myeclipse 在mac中字体模糊问题解决方案
找到文件:/Applications/MyEclipse 2014/MyEclipse 2014.app/Contents/Profile/myeclipse.app/Contents/Info.pl ...
- Redmine2.5+CentOS6+Apache2
redmine是使用ruby开发的一款无任何商业限制且可自行部署的项目管理软件,其简洁的界面比较符合程序猿的定位,使用起来比较方便,由于我之前装3X没 成功,各版本之间的依存和配置都不一样,所以最后参 ...
- delphi项目中的modelsupport文件夹
delphi项目中的modelsupport文件夹 今天写着写着突然发现多了一个这个文件夹..苦思不得其解 看着又难受 删了又重建 终于找到了 存此备查;Tools--option--toget ...
- P1159岳麓山上打水
P1159岳麓山上打水 https://vijos.org/p/1159 dfsID,第一次听说这东西,但是感觉不太靠谱啊. 一开始的时候,想到了排个序后,然后进行dp,如果要输出字典序最小其实还是可 ...
- centos 添加 composer
下载安装包 curl -sS https://getcomposer.org/installer | php 把 composer 把复制到 /usr/bin/composer mv composer ...