File tree Expand file tree Collapse file tree 4 files changed +125
-0
lines changed
construct-binary-tree-from-preorder-and-inorder-traversal
longest-palindromic-substring Expand file tree Collapse file tree 4 files changed +125
-0
lines changed Original file line number Diff line number Diff line change 1+ class TreeNode {
2+ val : number ;
3+ left : TreeNode | null ;
4+ right : TreeNode | null ;
5+ constructor ( val ?: number , left ?: TreeNode | null , right ?: TreeNode | null ) {
6+ this . val = val === undefined ? 0 : val ;
7+ this . left = left === undefined ? null : left ;
8+ this . right = right === undefined ? null : right ;
9+ }
10+ }
11+
12+ // TC: O(n)
13+ // SC: O(n)
14+ function buildTree ( preorder : number [ ] , inorder : number [ ] ) : TreeNode | null {
15+ const indices = { } ;
16+ // indices = {
17+ // 9: 0,
18+ // 3: 1,
19+ // 15: 2,
20+ // 20: 3,
21+ // 7: 4
22+ // }
23+
24+ for ( let i = 0 ; i < inorder . length ; i ++ ) {
25+ const num = inorder [ i ] ;
26+ indices [ num ] = i ;
27+ }
28+
29+ let preIndex = 0 ;
30+
31+ const dfs = ( start : number , end : number ) => {
32+ if ( start > end ) return null ;
33+ const val = preorder [ preIndex ] ;
34+ preIndex ++ ;
35+ const mid = indices [ val ] ;
36+
37+ const left = dfs ( start , mid - 1 ) ;
38+ const right = dfs ( mid + 1 , end ) ;
39+
40+ return new TreeNode ( val , left , right ) ;
41+ } ;
42+
43+ return dfs ( 0 , inorder . length - 1 ) ;
44+ }
Original file line number Diff line number Diff line change 1+ // TC: O(n^2)
2+ // SC: O(1)
3+ function longestPalindrome ( s : string ) : string {
4+ if ( s . length < 2 ) return s ;
5+
6+ let maxLeft = 0 ;
7+ let maxRight = 0 ;
8+
9+ const expandWindow = ( left : number , right : number ) : void => {
10+ while ( left >= 0 && right < s . length && s [ left ] === s [ right ] ) {
11+ if ( maxRight - maxLeft < right - left ) {
12+ maxRight = right ;
13+ maxLeft = left ;
14+ }
15+ left -- ;
16+ right ++ ;
17+ }
18+ } ;
19+
20+ for ( let i = 0 ; i < s . length ; i ++ ) {
21+ expandWindow ( i , i ) ; // odd length palindrome
22+ expandWindow ( i , i + 1 ) ; // even length palindrome
23+ }
24+
25+ return s . slice ( maxLeft , maxRight + 1 ) ;
26+ }
Original file line number Diff line number Diff line change 1+ /**
2+ Do not return anything, modify matrix in-place instead.
3+ */
4+
5+ // TC: O(n^2)
6+ // SC: O(1)
7+ function rotate ( matrix : number [ ] [ ] ) : void {
8+ let top = 0 ;
9+ let bottom = matrix . length - 1 ;
10+
11+ while ( top < bottom ) {
12+ let left = top ;
13+ let right = bottom ;
14+
15+ for ( let i = 0 ; i < bottom - top ; i ++ ) {
16+ const temp = matrix [ top ] [ left + i ] ; // topLeft
17+ matrix [ top ] [ left + i ] = matrix [ bottom - i ] [ left ] ;
18+ matrix [ bottom - i ] [ left ] = matrix [ bottom ] [ right - i ] ;
19+ matrix [ bottom ] [ right - i ] = matrix [ top + i ] [ right ] ;
20+ matrix [ top + i ] [ right ] = temp ;
21+ }
22+
23+ top ++ ; // top down
24+ bottom -- ; // bottom up
25+ }
26+ }
Original file line number Diff line number Diff line change 1+ class TreeNode {
2+ val : number ;
3+ left : TreeNode | null ;
4+ right : TreeNode | null ;
5+ constructor ( val ?: number , left ?: TreeNode | null , right ?: TreeNode | null ) {
6+ this . val = val === undefined ? 0 : val ;
7+ this . left = left === undefined ? null : left ;
8+ this . right = right === undefined ? null : right ;
9+ }
10+ }
11+
12+ // TC: O(m + n), m = number of nodes in root, n = number of nodes in subRoot
13+ // SC: O(m + n)
14+ function isSubtree ( root : TreeNode | null , subRoot : TreeNode | null ) : boolean {
15+ const serializeNode = ( node : TreeNode | null ) => {
16+ if ( ! node ) return "$" ;
17+
18+ const str = `(${ node . val } ,${ serializeNode ( node . left ) } ,${ serializeNode (
19+ node . right
20+ ) } )`;
21+
22+ return str ;
23+ } ;
24+
25+ const serializedRoot = serializeNode ( root ) ;
26+ const serializedSubRoot = serializeNode ( subRoot ) ;
27+
28+ return serializedRoot . includes ( serializedSubRoot ) ;
29+ }
You can’t perform that action at this time.
0 commit comments