二叉树的镜像

last modify

二叉树的镜像_牛客题霸_牛客网

问题简述

操作给定的二叉树,将其变换为源二叉树的镜像。

思路

  • 后序遍历,交换左右子节点;

Python
class Solution:
    def Mirror(self , pRoot: TreeNode) -> TreeNode:
        
        def dfs(x):
            if not x: return None
            x.right, x.left = dfs(x.left), dfs(x.right)
            return x
        
        return dfs(pRoot)

Last updated