Open bonfy opened 5 years ago
解法一: 递归 Recursive
时间复杂度 O(n), n 为 tree 的 node 数
# 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
Invert a binary tree.
Example:
Input:
Output:
Trivia:
Leetcode: 226. Invert Binary Tree - https://leetcode.com/problems/invert-binary-tree/