leetcode--226:(二叉树)反转二叉树
# 2019.7.12:
(慕名这题经典)
我的思路:
涉及二叉树的大部分需要递归,这就好办了,如果有root存在,先让root这边左、右节点交换,然后逐层交换直到叶子。
我的答案:
# Definition for a binary tree node. # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: def invertTree(self, root: TreeNode) -> TreeNode: if root: root.left, root.right = root.right, root.left self.invertTree(root.left) self.invertTree(root.right) return root