LeetCode 刷题记录: 101. Symmetric Tree [Python]

原题

https://leetcode.com/problems/symmetric-tree/

Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).

思路

递归判断左节点的左边是否等于右节点的右边。

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None


class Solution:
def isSymmetric(self, root: TreeNode) -> bool:
def isSym(L,R):
if not L and not R: return True
if L and R and L.val==R.val:
return isSym(L.left,R.right) and isSym(L.right,R.left)
return isSym(root,root)

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×