226. Invert Binary Tree | Java最短代码实现

原题链接: 226. Invert Binary Tree

【思路】

对于每个节点,交换左右子树,然后递归左右子树,这样就实现了数的反转:

    public TreeNode invertTree(TreeNode root) {
        if (root == null) return root;
        TreeNode tmp = root.left;
        root.left = root.right;
        root.right = tmp;
        invertTree(root.left);
        invertTree(root.right);
        return root;
    }
68 / 68  test cases passed. Runtime: 0 ms  Your runtime beats 21.92% of javasubmissions.
欢迎优化!

你可能感兴趣的:(java,LeetCode)