forked from neetcode-gh/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
0230-kth-smallest-element-in-a-bst.swift
44 lines (39 loc) · 1.19 KB
/
0230-kth-smallest-element-in-a-bst.swift
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* Definition for a binary tree node.
* public class TreeNode {
* public var val: Int
* public var left: TreeNode?
* public var right: TreeNode?
* public init() { self.val = 0; self.left = nil; self.right = nil; }
* public init(_ val: Int) { self.val = val; self.left = nil; self.right = nil; }
* public init(_ val: Int, _ left: TreeNode?, _ right: TreeNode?) {
* self.val = val
* self.left = left
* self.right = right
* }
* }
*/
class Solution {
var globalCount: Int = 1
var globalAnswer: Int = -1
var limit: Int = 0
// Guaranteed k <= #Nodes
func kthSmallest(_ root: TreeNode?, _ k: Int) -> Int {
limit = k
inorderTraversal(from: root)
return globalAnswer
}
private func inorderTraversal(from node: TreeNode?) {
// Base case
guard let node = node else { return }
// Recurse on left child
inorderTraversal(from: node.left)
// "visit" it
if globalCount == limit {
globalAnswer = node.val
}
globalCount += 1
// Recurse on right child
inorderTraversal(from: node.right)
}
}