[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 ...
随机推荐
- Linux系统man查询命令等级及意义
1:用户命令,可由任何人启动的 2:系统调用,由内核提供的函数 3:库函数 4:设备,/dev目录下的特殊文件 5:文件格式描述,例如/etc/passwd 6:游戏 7:杂项,例如宏命令包.惯例等 ...
- 解析利用wsdl.exe生成webservice代理类的详解
利用wsdl.exe生成webservice代理类:根据提供的wsdl生成webservice代理类1.开始->程序->Visual Studio 2005 命令提示2.输入如下红色标记部 ...
- 一个等待页面加载完毕的loading动画
1 html 部分 <!DOCTYPE html><html><head><meta http-equiv="Content-Type" ...
- linux编程中printf显示不加换行的缓冲问题
最近在编写linux网络编程时,总是遇到这样的事,程序逻辑没错误,但是程序运行到某个地方就停在那里了,后来才发现在prinrf()中加入换行能正常运行了,如“ printf("123&quo ...
- Reactjs 入门基础(一)
实例中我们引入了三个库: react.min.js .react-dom.min.js 和 browser.min.js: 1,react.min.js -React 的 核心库 2,react-do ...
- 解析工具Goson
/** * 解析申请分配座位席别 * @param json * @return */ public static TrainOrderResponse getTrainOrder(String js ...
- jquery ajax load
jQuery load() 方法 jQuery load() 方法是简单但强大的 AJAX 方法. load() 方法从服务器加载数据,并把返回的数据放入被选元素中. 语法: $(selector). ...
- Object转bigdecimal
/*由数字字符串构造BigDecimal的方法 *设置BigDecimal的小数位数的方法 */ import java.math.BigDecimal; //数字字符串 String StrBd=& ...
- java 链接jdbc
import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sq ...
- 关于form验证的处理片断
public virtual void SignIn(s_User user, bool createPersistentCookie) { var now = DateTime.UtcNow.ToL ...