965. 单值二叉树
为保证权益,题目请参考 965. 单值二叉树(From LeetCode).
解决方案1
Python
python
# 965. 单值二叉树
# https://leetcode.cn/problems/univalued-binary-tree/
# Definition for a binary tree node.
class TreeNode:
def __init__(self, val=0, left=None, right=None):
self.val = val
self.left = left
self.right = right
class Solution:
def isUnivalTree(self, root: TreeNode) -> bool:
def isIt(root:TreeNode, val:int) -> bool:
if root is None:
return True
else:
if root.val == val:
return isIt(root.left, val) and isIt(root.right, val)
else:
return False
return isIt(root, root.val)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25