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. PCI Express(二) - Topology

    原文出处:http://www.fpga4fun.com/PCI-Express2.html Point-to-point architecture At 2.5Gbps, the PCI Expre ...

  2. jQuery ajax()使用serialize()提交form数据

    jQuery的serialize()方法通过序列化表单值,创建URL编码文本字符串,我们就可以选择一个或多个表单元素,也可以直接选择form将其序列化 <form action="&q ...

  3. windows2008r2环境双实例安装mysql5.6

    windows2008r2环境双实例安装mysql5.6 环境:windows2008 r2 标准版 1.默认安装了一个mysql5.6端口为3306 2.使用msi文件安装需要.net4.0支持,安 ...

  4. Maven学习总结(二)——Maven项目构建过程练习

    上一篇只是简单介绍了一下maven入门的一些相关知识,这一篇主要是体验一下Maven高度自动化构建项目的过程 一.创建Maven项目 1.1.建立Hello项目 1.首先建立Hello项目,同时建立M ...

  5. 简单的javascript--test2

    插件: jquery, sweetalert (http://t4t5.github.io/sweetalert/ ) 1.index.html <!DOCTYPE HTML> <h ...

  6. mysql处理海量数据时的一些优化查询速度方法

      最近一段时间由于工作需要,开始关注针对Mysql数据库的select查询语句的相关优化方法. 由于在参与的实际项目中发现当mysql表的数据量达到百万级时,普通SQL查询效率呈直线下降,而且如果w ...

  7. java.net.URL 模拟用户登录网页并维持session

    java.net.URL 模拟用户登录网页并维持session 半成品,并非完全有用 import java.io.BufferedReader; import java.io.InputStream ...

  8. storyboard xib下label怎么自适应宽度高度

    先看需求:两个Label,要求蓝色的label紧跟在红色的label文字后面  ok首选正常添加约束 红色的Label添加宽度,高度,左边,上边约束 蓝色的Label添加宽度,高度,左边,和红色的水平 ...

  9. Python环境下NIPIR(ICTCLAS2014)中文分词系统使用攻略

    一.安装 官方链接:http://pynlpir.readthedocs.org/en/latest/installation.html 官方网页中介绍了几种安装方法,大家根据个人需要,自行参考!我采 ...

  10. WiFi破解

    BT5破解WPA2-PSK无线密码实践     笔者住处附近有无线信号,觉得可以试着破解一下,于是有了如下实践. 软硬件环境:Vmware Workstation 9,BT5 r3,reaver1.4 ...