美文网首页
226 Invert Binary Tree

226 Invert Binary Tree

作者: 烟雨醉尘缘 | 来源:发表于2019-02-18 13:52 被阅读0次

Invert a binary tree.

Example:

no example

解释下题目:

交换左右子树

1. 交换左右子树即可

实际耗时:0ms

public TreeNode invertTree(TreeNode root) {
        if (root != null) {
            TreeNode left = root.left;
            TreeNode right = root.right;
            root.left = right;
            root.right = left;
            invertTree(left);
            invertTree(right);
        }
        return root;
    }

  思路:老老实实换呗。

时间复杂度O(n) n为节点数
空间复杂度O(1)

相关文章

网友评论

      本文标题:226 Invert Binary Tree

      本文链接:https://www.haomeiwen.com/subject/hysaeqtx.html