LeetCode100题之—4、一翻转二叉树(python)
作者:互联网
翻转二叉树
题目描述
答案
步骤详解
分为两个步骤
1)利用二叉树的先序遍历
2)每次遍历都调整左右孩子的位置
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def reverse(self,root):
TreeNode=root.left
root.left=root.right
root.right=TreeNode
def invertTree(self, root):
"""
:type root: TreeNode
:rtype: TreeNode
"""
if root==None:
return
else:
self.reverse(root)
self.invertTree(root.left)
self.invertTree(root.right)
return root
标签:right,TreeNode,python,self,二叉树,left,root,LeetCode100 来源: https://blog.csdn.net/qq_39740279/article/details/113835820