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的更多相关文章

  1. 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 ...

  2. 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): ...

  3. 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 ...

  4. (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 ...

  5. 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 解题思路: 我只想说递归大法好. ...

  6. Leetcode 226 Invert Binary Tree python

    题目: Invert a binary tree. 翻转二叉树. 递归,每次对节点的左右节点调用invertTree函数,直到叶节点. python中也没有swap函数,当然你可以写一个,不过pyth ...

  7. 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 ...

  8. 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 ...

  9. LeetCode 226 Invert Binary Tree 解题报告

    题目要求 Invert a binary tree. 题目分析及思路 给定一棵二叉树,要求每一层的结点逆序.可以使用递归的思想将左右子树互换. python代码 # Definition for a ...

随机推荐

  1. winform客户端向web地址传参,怎样去接收参数。

    在web端定义js方法去接收客户端传递过来的参数,具体就是获取地址中?后的数据,各个参数用&分割,存储于数组中,获取. 具体如下: //定义获取地址中参数的方法 function GetReq ...

  2. 窗口 超类化 子类化 HOOK

    body { font-family: Bitstream Vera Sans Mono; font-size: 11pt; line-height: 1.5; } html, body { colo ...

  3. avalon2学习教程14动画使用

    avalon2实际上没有实现完整的动画模块,它只是对现有的CSS3动画或jquery animate再包装一层. 我们先说如何用CSS3为avalon实现动画效果.首先要使用avalon.effect ...

  4. 使用Jquery解析xml的两种方法

    第一种方案(最稳妥): 先将String格式的xml转换为xml对象,然后再用Jquery解析xml对象 var returnDataXml = parseXML(returnData); var p ...

  5. DataGridView控件“至少有一列没有单元格模板”问题处理

    这个问题一般是没有设置单元格模板造成的. mdgv.Columns[].CellTemplate = new DataGridViewTextBoxCell();

  6. decimalFormat("#","##0.00") java

    importjava.text.DecimalFormat; publicclassTestNumberFormat{ publicstaticvoidmain(String[]args){ doub ...

  7. iOS定位到崩溃代码行数

    不知道大家是不是在代码调试过程中经常遇到项目崩溃的情况: 比如: 数组越界: 没有实现方法选择器: 野指针: 还有很多很多情况.......昨天学到了一种可以直接定位到崩溃代码行数的一个命令,记录一下 ...

  8. php 通过变量 来调用函数

    <?php function fun() { echo 'fun'; } $a = 'fun'; $a(); ?> 复制代码 上面的$a变量就是fun()函数,调用$a()和调用fun() ...

  9. Genome-wide Complex Trait Analysis(GCTA)-全基因组复杂性状分析

    GCTA(全基因组复杂性状分析)工具开发目的是针对复杂性状的全基因组关联分析,评估SNP解释的表型方差所占的比例(该网站地址:http://cnsgenomics.com/software/gcta/ ...

  10. 手机驱动无法正常安装,出现adb interface失败

    手机一直无法用usb连接上电脑,试了各种方法,总是提示安装驱动失败,或者找不到文件. 在网上找了各种方法,后来结果证明,是我自己手贱了,... 方法: Win7系统用户已经碰到几次在安装adb驱动时提 ...