Skip to content

Commit 194a736

Browse files
committed
Time: 48 ms (51.17%), Space: 16.8 MB (49.94%) - LeetHub
1 parent 688b2be commit 194a736

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# time complexity: O(n)
2+
# space complexity: O(n)
3+
from typing import Optional
4+
5+
6+
class TreeNode:
7+
def __init__(self, val=0, left=None, right=None):
8+
self.val = val
9+
self.left = left
10+
self.right = right
11+
12+
13+
class Solution:
14+
def evaluateTree(self, root: Optional[TreeNode]) -> bool:
15+
if not root.left and not root.right:
16+
return root.val != 0
17+
evalRootLeft = self.evaluateTree(root.left)
18+
evalRootRight = self.evaluateTree(root.right)
19+
20+
if root.val == 2:
21+
evalRoot = evalRootLeft or evalRootRight
22+
else:
23+
evalRoot = evalRootLeft and evalRootRight
24+
return evalRoot
25+
26+
27+
root = TreeNode(2)
28+
root.left = TreeNode(1)
29+
root.right = TreeNode(3)
30+
root.right.left = TreeNode(0)
31+
root.right.right = TreeNode(1)
32+
print(Solution().evaluateTree(root))

0 commit comments

Comments
 (0)