Invert a binary tree.

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

to

     4
/ \
7 2
/ \ / \
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 互换

 /**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution
{
public TreeNode invertTree(TreeNode root)
{
if(root == null)
return null; // continue children
invertTree(root.left);
invertTree(root.right); // swap left and right
TreeNode temp;
temp = root.left;
root.left = root.right;
root.right = temp; return root;
}
}

参考资料: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. iScroll在谷歌浏览器中的问题

    通常情况下,我们会使用iScroll.js做移动端的下拉刷新和上拉加载功能,当然,还有很多其他功能. 不过,在使用iScroll的时候,在谷歌浏览器中出现不支持的情况,即,做移动的时候,出现卡顿或是每 ...

  2. hadoop-2.6.0源码编译

    运行hadoop环境时,常常会出现这种提示 WARN util.NativeCodeLoader: Unable to load native-hadoop library for your plat ...

  3. Mysql双机热备配置(超详细多图版)

    一.双击热备介绍 1.基本概念 双机热备特指基于高可用系统中的两台服务器的热备(或高可用),双机高可用按工作中的切换方式分为:主-备方式(Active-Standby方式)和双主机方式(Active- ...

  4. idea下使用autowire注解注入对象,结果初始化不到类

    如果idea下使用autowire注解注入对象,结果初始化不到类,明明使用快捷键alt+insert是可以找到该注入的对象的. 而我们在使用的时候,缺报错了??? 注意,当我们在注入对象的时候,我们留 ...

  5. Spring配置文件的命名空间URI

    Spring配置文件介绍 <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=" ...

  6. Spring-mvc配置“/”路径过滤问题

    首先大家都知道"/*"可以匹配所有url,包括带扩展名的,一般只用在过滤器上. 一般Spring-mvc的核心 <servlet> <servlet-name&g ...

  7. 利用GPU实现无尽草地的实时渲染

    0x00 前言 在游戏中展现一个写实的田园场景时,草地的渲染是必不可少的,而一提到高效率的渲染草地,很多人都会想起GPU Gems第七章 <Chapter 7. Rendering Countl ...

  8. 中国移动飞信WAP登陆分析及脚本

    中国移动飞信WAP网页版 http://f.10086.cn/im5/ 用WAP飞信登录并向好友发送信息,同时用wireshark抓包. 1.过滤POST表单提交数据包(wireshark规则: ht ...

  9. 深入理解计算机系统chapter5

    编写高效的程序需要:1.选择合适的数据结构和算法 2.编译器能够有效优化以转换为高效可执行代码的源代码 3.利用并行性 优化编译器的局限性 程序示例: combine3的汇编代码: load-> ...

  10. hibernate学习手记(1)

    1. java.sql.SQLException: The server time zone value '?й???????' is unrecognized or represents more ...