LeetCode 226 Invert Binary Tree 解题报告
作者:互联网
题目要求
Invert a binary tree.
题目分析及思路
给定一棵二叉树,要求每一层的结点逆序。可以使用递归的思想将左右子树互换。
python代码
# 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 not root:
return
right = self.invertTree(root.right)
left = self.invertTree(root.left)
root.left = right
root.right = left
return root
标签:Binary,right,TreeNode,invertTree,Invert,Tree,root,self,left 来源: https://www.cnblogs.com/yao1996/p/10504383.html