File tree Expand file tree Collapse file tree 2 files changed +64
-0
lines changed
kth-smallest-element-in-a-bst Expand file tree Collapse file tree 2 files changed +64
-0
lines changed Original file line number Diff line number Diff line change 1+ // 시간 복잡도: O(n)
2+ // 공간 복잡도: O(n)
3+
4+ /**
5+ * Definition for a binary tree node.
6+ * function TreeNode(val, left, right) {
7+ * this.val = (val===undefined ? 0 : val)
8+ * this.left = (left===undefined ? null : left)
9+ * this.right = (right===undefined ? null : right)
10+ * }
11+ */
12+ /**
13+ * @param {TreeNode } root
14+ * @param {number } k
15+ * @return {number }
16+ */
17+ var kthSmallest = function ( root , k ) {
18+ const results = [ ]
19+
20+ const dfs = ( tree ) => {
21+ results . push ( tree . val )
22+ if ( tree . left ) dfs ( tree . left )
23+ if ( tree . right ) dfs ( tree . right )
24+ }
25+
26+ dfs ( root )
27+
28+ return results [ k - 1 ]
29+ } ;
Original file line number Diff line number Diff line change 1+ // 시간 복잡도: O(n^3)
2+ // 공간 복잡도: O(1)
3+
4+ /**
5+ * @param {string } s
6+ * @return {number }
7+ */
8+ var countSubstrings = function ( s ) {
9+ let count = 0 ;
10+
11+ let plusIndex = 0 ;
12+ while ( plusIndex !== s . length ) {
13+ for ( let i = 0 ; i < s . length - plusIndex ; i ++ ) {
14+ if ( isValidSubstring ( s , i , i + plusIndex ) ) count ++
15+ }
16+
17+ plusIndex ++ ;
18+ }
19+
20+ return count ;
21+ } ;
22+
23+
24+ function isValidSubstring ( s , left , right ) {
25+ while ( left <= right ) {
26+ if ( s [ left ] !== s [ right ] ) return false ;
27+
28+ left ++ ;
29+ right -- ;
30+ }
31+ return true ;
32+ }
33+
34+ console . log ( countSubstrings ( "abc" ) ) ;
35+ console . log ( countSubstrings ( "aaa" ) ) ;
You can’t perform that action at this time.
0 commit comments