File tree Expand file tree Collapse file tree 3 files changed +94
-0
lines changed
kth-smallest-element-in-a-bst
lowest-common-ancestor-of-a-binary-search-tree Expand file tree Collapse file tree 3 files changed +94
-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+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간 복잡도: 트리의 한쪽 경로를 선택하면서 탐색하므로, 트리의 높이가 h면 O(h)
3+ * 공간 복잡도: 재귀 호출 스택의 깊이는 균형 잡힌 트리의 경우 O(logn), 편향된 트리는 O(n) ==> O(h)
4+ */
5+ /**
6+ * Definition for a binary tree node.
7+ * function TreeNode(val) {
8+ * this.val = val;
9+ * this.left = this.right = null;
10+ * }
11+ */
12+ /**
13+ * @param {TreeNode } root
14+ * @param {TreeNode } p
15+ * @param {TreeNode } q
16+ * @return {TreeNode }
17+ */
18+ var lowestCommonAncestor = function ( root , p , q ) {
19+ const dfs = ( node , p , q ) => {
20+ if ( node . val > p . val && node . val > q . val ) {
21+ return dfs ( node . left , p , q ) ;
22+ }
23+ if ( node . val < p . val && node . val < q . val ) {
24+ return dfs ( node . right , p , q ) ;
25+ }
26+ return node ;
27+ }
28+ return dfs ( root , p , q ) ;
29+ } ;
30+
Original file line number Diff line number Diff line change 1+ /**
2+ * 시간 복잡도: 정렬의 시간 복잡도와 같음. O(nlogn)
3+ * 공간 복잡도: 자바스크립트의 정렬은 Timsort를 사용. 따라서 최악의 경우 공간 복잡도는 O(n)
4+ */
5+ /**
6+ * Definition of Interval:
7+ * class Interval {
8+ * constructor(start, end) {
9+ * this.start = start;
10+ * this.end = end;
11+ * }
12+ * }
13+ */
14+
15+ class Solution {
16+ /**
17+ * @param {Interval[] } intervals
18+ * @returns {boolean }
19+ */
20+ canAttendMeetings ( intervals ) {
21+ intervals . sort ( ( a , b ) => a . start - b . start )
22+
23+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
24+ if ( intervals [ i ] . start < intervals [ i - 1 ] . end ) {
25+ return false ;
26+ }
27+ }
28+ return true ;
29+ }
30+ }
You can’t perform that action at this time.
0 commit comments