File tree Expand file tree Collapse file tree 3 files changed +112
-0
lines changed
maximum-depth-of-binary-tree Expand file tree Collapse file tree 3 files changed +112
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for a binary tree node.
3+ * class TreeNode {
4+ * val: number
5+ * left: TreeNode | null
6+ * right: TreeNode | null
7+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
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+ /**
15+ * ํธ๋ฆฌ์ ๊น์ด ๊ตฌํ๊ธฐ
16+ * ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋
17+ * - ์๊ฐ ๋ณต์ก๋: O(n)
18+ * - ๊ณต๊ฐ ๋ณต์ก๋: O(n)
19+ * @param root
20+ */
21+ function maxDepth ( root : TreeNode | null ) : number {
22+ if ( ! root ) return 0 ;
23+ let max = 0 ;
24+ let stack : [ TreeNode | null , number ] [ ] = [ [ root , 1 ] ] ;
25+ while ( stack . length > 0 ) {
26+ const [ node , depth ] = stack . pop ( ) ;
27+ max = Math . max ( depth , max ) ;
28+ if ( node . left ) stack . push ( [ node . left , depth + 1 ] ) ;
29+ if ( node . right ) stack . push ( [ node . right , depth + 1 ] ) ;
30+ }
31+ return max
32+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * ๊ฒน์น๋ ๊ตฌ๊ฐ ํฉ์น๊ธฐ
3+ * ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋
4+ * - ์๊ฐ ๋ณต์ก๋: O(n log n) - sort() ์ํฅ
5+ * - ๊ณต๊ฐ ๋ณต์ก๋: O(n)
6+ * @param intervals
7+ */
8+ function merge ( intervals : number [ ] [ ] ) : number [ ] [ ] {
9+ if ( intervals . length === 1 ) return intervals
10+
11+ intervals . sort ( ( a , b ) => a [ 0 ] - b [ 0 ] ) ;
12+
13+ let results : number [ ] [ ] = [ ]
14+ let [ tempX , tempY ] = intervals [ 0 ]
15+
16+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
17+ let [ x , y ] = intervals [ i ]
18+ if ( x <= tempY ) {
19+ tempY = Math . max ( tempY , y )
20+ } else {
21+ results . push ( [ tempX , tempY ] )
22+ tempX = x
23+ tempY = y
24+ }
25+ }
26+ results . push ( [ tempX , tempY ] )
27+
28+ return results ;
29+ }
Original file line number Diff line number Diff line change 1+ /**
2+ * Definition for singly-linked list.
3+ * class ListNode {
4+ * val: number
5+ * next: ListNode | null
6+ * constructor(val?: number, next?: ListNode | null) {
7+ * this.val = (val===undefined ? 0 : val)
8+ * this.next = (next===undefined ? null : next)
9+ * }
10+ * }
11+ */
12+
13+ /**
14+ Do not return anything, modify head in-place instead.
15+ */
16+
17+ /**
18+ * ๋ฆฌ์คํธ ์ฌ์ ๋ ฌ ํ๊ธฐ (0 -> n -> 1 -> n-1 -> ...)
19+ * ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋
20+ * - ์๊ฐ ๋ณต์ก๋: O(n)
21+ * - ๊ณต๊ฐ ๋ณต์ก๋: O(n)
22+ * @param head
23+ */
24+ function reorderList ( head : ListNode | null ) : void {
25+ if ( ! head || ! head . next ) return ;
26+
27+ const stack : ListNode [ ] = [ ] ;
28+ let node = head ;
29+ while ( node ) {
30+ stack . push ( node ) ;
31+ node = node . next ;
32+ }
33+
34+ let left = 0 ;
35+ let right = stack . length - 1 ;
36+
37+ while ( left < right ) {
38+ // ํ์ฌ ๋
ธ๋์ ๋ค์์ ๋ง์ง๋ง ๋
ธ๋ ์ฐ๊ฒฐ
39+ stack [ left ] . next = stack [ right ] ;
40+ left ++ ;
41+
42+ // ๋จ์ ๋
ธ๋๊ฐ ์์ผ๋ฉด ๋ง์ง๋ง ๋
ธ๋์ ๋ค์์ ๋ค์ ์ผ์ชฝ ๋
ธ๋ ์ฐ๊ฒฐ
43+ if ( left < right ) {
44+ stack [ right ] . next = stack [ left ] ;
45+ right -- ;
46+ }
47+ }
48+
49+ // ๋ง์ง๋ง ๋
ธ๋์ next๋ฅผ null๋ก ์ค์
50+ stack [ left ] . next = null ;
51+ }
You canโt perform that action at this time.
0 commit comments