Invert a binary tree.

  1. 4
  2. / \
  3. 2 7
  4. / \ / \
  5. 1 3 6 9

to

  1. 4
  2. / \
  3. 7 2
  4. / \ / \
  5. 9 6 3 1

Trivia:
This problem was inspired by this original tweet by Max Howell:

Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on a whiteboard so fuck off.


题目标签:Tree

  这道题目给了我们一个二叉树,让我们把它反转一下。对于每一个点,它的左边和右边子树需要对调一下。利用postOrder 来遍历树,当走到最低端的时候,点 == null, 返回null, 对于每一个点,把它的left 和right 互换一下。return 这个点。

Java Solution:

Runtime beats 27.97%

完成日期:07/04/2017

关键词:Tree

关键点:把left 和right 互换

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. public class Solution
  11. {
  12. public TreeNode invertTree(TreeNode root)
  13. {
  14. if(root == null)
  15. return null;
  16.  
  17. // continue children
  18. invertTree(root.left);
  19. invertTree(root.right);
  20.  
  21. // swap left and right
  22. TreeNode temp;
  23. temp = root.left;
  24. root.left = root.right;
  25. root.right = temp;
  26.  
  27. return root;
  28. }
  29. }

参考资料:N/A

LeetCode 算法题目列表 - LeetCode Algorithms Questions List

LeetCode 226. Invert Binary 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 Tri ...

  2. 【easy】226. Invert Binary Tree 反转二叉树

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  3. Leetcode 226 Invert Binary Tree python

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

  4. 【LeetCode】226. Invert Binary Tree 翻转二叉树(Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 递归 迭代 日期 题目地址: https://lee ...

  5. LeetCode 226 Invert Binary Tree(转换二叉树)

    翻译 将下图中上面的二叉树转换为以下的形式.详细为每一个左孩子节点和右孩子节点互换位置. 原文 如上图 分析 每次关于树的题目出错都在于边界条件上--所以这次细致多想了一遍: void swapNod ...

  6. LeetCode Invert Binary Tree 反转二叉树

    思路:递归解决,在返回root前保证该点的两个孩子已经互换了.注意可能给一个Null. C++ /** * Definition for a binary tree node. * struct Tr ...

  7. Leetcode 226 Invert Binary Tree 二叉树

    交换左右叶子节点 /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * ...

  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. java: Multiple encodings set for module chunk test "GBK" will be used by compiler

    IDEA 进行编译代码的时候,特别是新项目 特别容易出现 编码错误,但是 File-Encoding中设置的又没有问题,而且maven 是能打包的,就是用 idea 自带的 编译的时候 就会出现提示 ...

  2. JS判斷文件大小

    function findSize(file) { var dom = document.getElementById("file"); var fileSize = dom.fi ...

  3. Mysql查询优化小结

    转自http://www.cnblogs.com/112ba/p/6220650.html 数据类型 简单原则:更小更好,简单就好,避免NULL1)整型如int(10)括号中的值与存储大小无关2)实数 ...

  4. Ubuntu16.04下安装mysql

    系统信息 (lsb_release -a) Distributor ID: Ubuntu Description: Ubuntu 16.04.2 LTS Release: 16.04 Codename ...

  5. java集合系列——List集合之Vector介绍(四)

    1. Vector的简介 JDK1.7.0_79版本 Vector 类可以实现可增长的对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.但是,Vector 的大小可以根据需要增大或缩小,以适 ...

  6. js中+号的另外一种用法

    JavaScript中可以在某个元素前使用 ‘+’ 号,这个操作是将该元素转换成Number类型,如果转换失败,那么将得到 NaN. 所以 +new Date 将会调用 Date.prototype ...

  7. leetCode没那么难啦 in Java (二)

    介绍    本篇介绍的是标记元素的使用,很多需要找到正确元素都可以将正确元素应该插入的位置单独放置一个标记来记录,这样可以达到原地排序的效果. Start 27.RemoveElement 删除指定元 ...

  8. Digital Square 搜索

    Digital Square Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Subm ...

  9. centos crontab(定时任务) 使用

    一.介绍   crontab命令的功能是在一定的时间间隔调度一些命令的执行.当安装完成操作系统之后,默认便会启动此任务调度命令.crond命令每分锺会定期检查是否有要执行的工作,如果有要执行的工作便会 ...

  10. Tomcat服务器如何读取本地磁盘数据?

    实际问题: 如何让用户下载本地磁盘的资源文件呢?  在server.xml文件中配置虚拟路径如下(以下代码放在Host标签之中即可): 例如: 具体含义: 把本地磁盘目录 "D:\uploa ...