【每日阅读】2020年11月9日-反转二叉树

https://leetcode-cn.com/problems/invert-binary-tree/

今天看到这个题下面有个注解:

这个问题是受到 Max Howell 的 原问题 启发的 :

谷歌:我们90%的工程师使用您编写的软件(Homebrew),但是您却无法在面试时在白板上写出翻转二叉树这道题,这太糟糕了。

就是homebrew的作者去google面试因这道题被拒。真的是笑死人了哈哈哈,原来大佬也会阴沟翻船啊。

这道题其实特别简单,会前序遍历就会这道题。当然,很久没使用树的话却是可能出现大佬这种局面。下面是我的代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
    public TreeNode invertTree(TreeNode root) {
        reverse(root);

        return root;
    }

    public void reverse(TreeNode root) {
        if (root == null) {
            return;
        }

        TreeNode left = root.left;
        root.left = root.right;
        root.right = left;

        reverse(root.left);
        reverse(root.right);
    }
}

原创文章,作者:geekgao,如若转载,请注明出处:https://www.geekgao.cn/archives/2675

(0)
geekgaogeekgao博主
上一篇 2020年11月5日
下一篇 2020年11月10日

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

GitHub
分享本页
返回顶部

Warning: error_log(/usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/#log/log-0206.txt): failed to open stream: No such file or directory in /usr/local/lighthouse/softwares/wordpress/wp-content/plugins/spider-analyser/spider.class.php on line 2900