File tree 1 file changed +34
-0
lines changed
kth-smallest-element-in-a-bst 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * ์๊ฐ ๋ณต์ก๋: k๋ฒ์งธ ์๊ฐ ๊ฐ์ฅ ํฐ ์์ผ ๋๋ ์ต์
์ ๊ฒฝ์ฐ์ด๋ฉฐ, ๋ชจ๋ ๋
ธ๋๋ฅผ ๋ฐฉ๋ฌธํ๋ฏ๋ก O(n)
3
+ * ๊ณต๊ฐ ๋ณต์ก๋: ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ ๊ท ํ ์กํ ํธ๋ฆฌ์ ๊ฒฝ์ฐ O(logn), ํธํฅ๋ ํธ๋ฆฌ๋ O(n)
4
+ */
5
+ /**
6
+ * Definition for a binary tree node.
7
+ * function TreeNode(val, left, right) {
8
+ * this.val = (val===undefined ? 0 : val)
9
+ * this.left = (left===undefined ? null : left)
10
+ * this.right = (right===undefined ? null : right)
11
+ * }
12
+ */
13
+ /**
14
+ * @param {TreeNode } root
15
+ * @param {number } k
16
+ * @return {number }
17
+ */
18
+ var kthSmallest = function ( root , k ) {
19
+ let kth ;
20
+ let cnt = k ;
21
+
22
+ const dfs = ( node ) => {
23
+ if ( ! node ) return ;
24
+ dfs ( node . left ) ;
25
+ cnt -- ;
26
+ if ( cnt === 0 ) {
27
+ kth = node . val
28
+ return ;
29
+ }
30
+ dfs ( node . right ) ;
31
+ }
32
+ dfs ( root )
33
+ return kth ;
34
+ }
You canโt perform that action at this time.
0 commit comments