LeetCode 226 Invert Binary Tree (Python)

Posted by 小明MaxMing on June 1, 2020

题目

Invert a binary tree.

解题思路

使用递归的方法,假设子树已经操作完成,交换左右子树

代码

class Solution:
    def invertTree(self, root: TreeNode) -> TreeNode:
        if root:
            root.left, root.right = self.invertTree(root.right), self.invertTree(root.left)
        return root

视频讲解 YouTube<--欢迎点击订阅

视频讲解 bilibili<--欢迎点击订阅