We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 24ba0c6 commit 342f8d8Copy full SHA for 342f8d8
1 file changed
kth-smallest-element-in-a-bst/doh6077.py
@@ -8,19 +8,16 @@ class Solution:
8
# Time Complexity O(N)
9
def kthSmallest(self, root: Optional[TreeNode], k: int) -> int:
10
ans = []
11
- final = [0]
12
-
+ final = 0
13
def dfs(node):
14
+ nonlocal final
15
if not node:
16
return
17
18
dfs(node.left)
19
20
ans.append(node.val)
21
if len(ans) == k:
22
- final[0] = node.val
+ final = node.val
23
if len(ans) < k:
24
dfs(node.right)
25
dfs(root)
26
- return final[0]
+ return final
0 commit comments