背景描述

Homebrew 是 OS X 平台上的包管理工具。用其官网的话说就是:

the missing package manager for OS X | OS X 平台遗失的包管理器。

相信在用 Mac 的程序员,差不多都知道 Homebrew。

Homebrew 的开发者是 Max Howell。今天 Max 在推特发帖:

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.

谷歌:虽然我们 90% 工程师都在用你写的软件(Homebrew),但你不能在白板上反转二叉树,所以滚蛋。

这条推在开发者圈内引发热议。有网友认为,谷歌该改改面试流程了。

##

## 所以我把这道题自己做出来了,感觉很有成就感 ##

题目描述:

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.

思路分析:

相这种算法题目,我的思路是尽量用递归解决,因为二叉树本身就是一个递归的结构,同时注意判断空值判断,以及返回值

代码:

/**
 * 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) {
            TreeNode tmp = null;
            tmp = root.right;
            root.right = root.left;
            root.left = tmp;
            invertTree(root.left);
            invertTree(root.right);
            return root;
        }
        return null;
    }
}

leetCode之旅(12)-反转二叉树的更多相关文章

  1. LeetCode Invert Binary Tree 反转二叉树

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

  2. leetCode题解之反转二叉树

    1.题目描述 经典的反转二叉树,就是将二叉树中每个节点的左.右儿子交换. 2.题目分析 3.代码 TreeNode* invertTree(TreeNode* root) { if(root == N ...

  3. LeetCode OJ: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 ...

  4. leetcode之旅(11)-Integer to Roman

    题目描述: Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range fr ...

  5. LeetCode之旅(13)-Valid Anagram

    题目介绍: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

  6. 【LeetCode题解】7_反转整数

    目录 [LeetCode题解]7_反转整数 描述 方法一 思路 Java 实现 类似的 Java 实现 Python 实现 方法二:转化为求字符串的倒序 Java 实现 Python 实现 [Leet ...

  7. A1102 | 反转二叉树

    #include <stdio.h> #include <memory.h> #include <math.h> #include <string> # ...

  8. 【python】Leetcode每日一题-反转链表 II

    [python]Leetcode每日一题-反转链表 II [题目描述] 给你单链表的头节点 head 和两个整数 left 和 right ,其中 left <= right .请你反转从位置 ...

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

随机推荐

  1. 电脑hash破解

    我一直在想,到底用什么样的方式才能较长时间地控制已经得到了权限的目标呢?留后门,种木马,甚至是Rootkit?留的Webshell痕迹太明显,哪怕是一句话的Webshell,都极容易被管理员清除.放了 ...

  2. Microsoft Dynamics CRM2011 更换Logo

    之前操作过但没做过记录,这里记录下以防以后有需要时记不起来还有迹可循 IE收藏栏的图标,在网站根目录下的的/favicon.ico CRM网页中的Logo,替换/_imgs/crmmastheadlo ...

  3. java多线程的编程实例

    java中可有两种方式实现多线程: 一种是继承Thread类: 一种是实现Runnable接口: Thread类 是在java.lang包中定义的.一个类只要继承了Thread类同时覆写了本类中的ru ...

  4. Cocos2D实现RPG游戏人物地图行走的跟随效果

    大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处. 如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;) 在一些RPG游戏中,人物队列在地图中行走的时候有时需要实现一个 ...

  5. 简单RPC实现之Netty实现

    所谓RPC就是远程方法调用(Remote  Process Call ),简单的来说就是通过MQ,TCP,HTTP或者自己写的网络协议来传输我要调用对方的什么接口,对方处理之后再把结果返回给我.就这么 ...

  6. Fedora 20: How to enable SSH

    1. systemctl  enable sshd.service 2. service sshd restart

  7. ElGamal密码

    ElGamal也是一种基于离散对数的公钥体制,与Diffie-Hellman密钥体制密切相关.ElGamal密码体系用于数字签名标准(DSS)和S/MIME电子邮件标准等一些技术标准中. 算法描述: ...

  8. Mybatis插入MySQL数据库中文乱码

    Mybatis插入MySQL数据库中文乱码 在dataSource.properties配置文件中设置useUnicode=true&characterEncoding=utf-8编码即可. ...

  9. Cocos2D:塔防游戏制作之旅(十七)

    getHpDamage方法当敌人到达基地时被调用.你需要添加该方法到Enemy.m的update:方法中去,以便检查当敌人到达基地是会发生什么.幸运的是,你已经在之前的代码中实现这些了,你可以接着往下 ...

  10. 图文浅析Binder机制

    总述: Binder是Android系统提供的一种IPC机制,Android系统基本就可以看做基于Binder的C/S架构,Binder也是C/S形式出现,它属于驱动但是驱动的一段内存而不是设备,框架 ...