Skip to content

Commit 77872d4

Browse files
authored
Create insufficient-nodes-in-root-to-leaf-paths.py
1 parent 7f3316d commit 77872d4

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Time: O(n)
2+
# Space: O(h)
3+
4+
# Definition for a binary tree node.
5+
class TreeNode(object):
6+
def __init__(self, x):
7+
self.val = x
8+
self.left = None
9+
self.right = None
10+
11+
12+
class Solution(object):
13+
def sufficientSubset(self, root, limit):
14+
"""
15+
:type root: TreeNode
16+
:type limit: int
17+
:rtype: TreeNode
18+
"""
19+
if not root:
20+
return None
21+
if not root.left and not root.right:
22+
return None if root.val < limit else root
23+
root.left = self.sufficientSubset(root.left, limit-root.val)
24+
root.right = self.sufficientSubset(root.right, limit-root.val)
25+
if not root.left and not root.right:
26+
return None
27+
return root

0 commit comments

Comments
 (0)