剑指offer-二叉树的镜像(python)

我们思考递归的时候一定不要去一步一步看它执行了啥,只会更绕。
事实上这题不难,但是我不熟悉二叉树,题目见过了,下次就没什么问题了。

# class TreeNode:
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None
class Solution:
    # 返回镜像树的根节点
    def Mirror(self, root):
        # write code here
        if not root:
            return None
        root.left,root.right=root.right,root.left
        self.Mirror(root.left)
        self.Mirror(root.right)
        return root
发布了69 篇原创文章 · 获赞 46 · 访问量 5272

猜你喜欢

转载自blog.csdn.net/qq_42738654/article/details/104232627