给你一棵二叉树的根节点 root
,翻转这棵二叉树,并返回其根节点。
示例 1:
输入:root = [4,2,7,1,3,6,9]
输出:[4,7,2,9,6,3,1]
# [226] 翻转二叉树
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def invertTree(self, root: TreeNode) -> TreeNode:
def traversal(root: TreeNode):
if root == None:
return
rev=root.left
root.left=root.right
root.right=rev
traversal(root.left) # 左
traversal(root.right) # 右
traversal(root)
return root
# @lc code=end