天天看点

【LeetCode】226. Invert Binary Tree

问题描述

问题链接:https://leetcode.com/problems/invert-binary-tree/#/description

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.

我的代码

看了这个描述觉得Google也是逗啊,哈哈。

/**
 * 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 root;
        }
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;

        invertTree(root.left);
        invertTree(root.right);

        return root;
    }
}
           

打败了30.77%的Java代码,来看看讨论区。

讨论区

Straightforward DFS recursive, iterative, BFS solutions

链接地址:https://discuss.leetcode.com/topic/16039/straightforward-dfs-recursive-iterative-bfs-solutions

用了栈和BFS,感觉挺新鲜。

As in many other cases this problem has more than one possible solutions:

Lets start with straightforward - recursive DFS - it’s easy to write and pretty much concise.

public class Solution {
    public TreeNode invertTree(TreeNode root) {

        if (root == null) {
            return null;
        }

        final TreeNode left = root.left,
                right = root.right;
        root.left = invertTree(right);
        root.right = invertTree(left);
        return root;
    }
}
           

The above solution is correct, but it is also bound to the application stack, which means that it’s no so much scalable - (you can find the problem size that will overflow the stack and crash your application), so more robust solution would be to use stack data structure.

public class Solution {
    public TreeNode invertTree(TreeNode root) {

        if (root == null) {
            return null;
        }

        final Deque<TreeNode> stack = new LinkedList<>();
        stack.push(root);

        while(!stack.isEmpty()) {
            final TreeNode node = stack.pop();
            final TreeNode left = node.left;
            node.left = node.right;
            node.right = left;

            if(node.left != null) {
                stack.push(node.left);
            }
            if(node.right != null) {
                stack.push(node.right);
            }
        }
        return root;
    }
}
           

Finally we can easly convert the above solution to BFS - or so called level order traversal.

public class Solution {
    public TreeNode invertTree(TreeNode root) {

        if (root == null) {
            return null;
        }

        final Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);

        while(!queue.isEmpty()) {
            final TreeNode node = queue.poll();
            final TreeNode left = node.left;
            node.left = node.right;
            node.right = left;

            if(node.left != null) {
                queue.offer(node.left);
            }
            if(node.right != null) {
                queue.offer(node.right);
            }
        }
        return root;
    }
}
           

If I can write this code, does it mean I can get job at Google? ;)