@Lin--
2020-02-17T12:13:20.000000Z
字数 546
阅读 423
Leetcode
翻转左右子树:只需要将左子树指针指向右指针,有指针指向左指针,不断递归,直到叶子结点。
'''# File: invertTree.py# Author: 0HP# Date: 20200217# Purpose: solve the problem in website:https://leetcode.com/problems/invert-binary-tree/'''# Definition for a binary tree node.class TreeNode:def __init__(self, x):self.val = xself.left = Noneself.right = Noneclass Solution:def invertTree(self, root: TreeNode) -> TreeNode:if root==None:return Noneroot.left,root.right=root.right,root.leftif not root.left==None:root.left=self.invertTree(root.left)if not root.right==None:root.right=self.invertTree(root.right)return root