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

来源:互联网 发布:上海证券交易软件下载 编辑:程序博客网 时间:2024/05/20 13:39
原题链接: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.
欢迎优化!

1 0