zl程序教程

您现在的位置是:首页 >  其他

当前栏目

镜像树_18

2023-04-18 16:10:24 时间

思路:逐层遍历交换左右结点

参考图

我的代码:

public TreeNode Mirror(TreeNode pRoot) {
        if (pRoot == null || (pRoot.left == null && pRoot.right == null)) {
            return pRoot;
        }
            TreeNode temp = pRoot.left;
            pRoot.left = pRoot.right;
            pRoot.right = temp;
            Mirror(pRoot.left);
            Mirror(pRoot.right);
            return pRoot;
    }