力扣 226. 翻转二叉树

前置

1
2
3
4
5
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right

递归

1
2
3
4
5
6
def invertTree(root):
if not root: return root
invertTree(root.left)
invertTree(root.right)
root.left, root.right = root.right, root.left
return root
  • Copyright: Copyright is owned by the author. For commercial reprints, please contact the author for authorization. For non-commercial reprints, please indicate the source.
  • Copyrights © 2022 eightyninth
  • Visitors: | Views:

请我喝杯咖啡吧~

支付宝
微信