File tree Expand file tree Collapse file tree 4 files changed +139
-0
lines changed
non-overlapping-intervals
remove-nth-node-from-end-of-list
serialize-and-deserialize-binary-tree Expand file tree Collapse file tree 4 files changed +139
-0
lines changed Original file line number Diff line number Diff line change
1
+ /*
2
+ Time complexity: O(nlogn)
3
+ Space complexity: O(n)
4
+ */
5
+ function eraseOverlapIntervals ( intervals : number [ ] [ ] ) : number {
6
+ intervals . sort ( ( a , b ) => a [ 1 ] - b [ 1 ] )
7
+ let cnt = 0
8
+ let end = intervals [ 0 ] [ 1 ]
9
+ for ( let i = 1 ; i < intervals . length ; i ++ ) {
10
+ if ( intervals [ i ] [ 0 ] < end ) {
11
+ cnt ++
12
+ } else {
13
+ end = intervals [ i ] [ 1 ]
14
+ }
15
+ }
16
+ return cnt
17
+ } ;
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
+ Time complexity: O(n)
14
+ Space complexity: O(h)
15
+ */
16
+ function removeNthFromEnd ( head : ListNode | null , n : number ) : ListNode | null {
17
+ if ( head == null ) return head
18
+ const listLength = ( node : ListNode | null ) : number => {
19
+ if ( node == null ) return 0
20
+ return 1 + listLength ( node . next )
21
+ }
22
+ const nodeLen = listLength ( head )
23
+ let dummy : ListNode = new ListNode ( 0 , head ) ;
24
+ let node = dummy
25
+ for ( let i = 0 ; i < nodeLen - n ; i ++ ) {
26
+ if ( node . next ) {
27
+ node = node . next
28
+ }
29
+ }
30
+ node . next = node . next . next
31
+ return dummy . next
32
+ } ;
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
+ Time complexity: O(m)
16
+ Space complexity: O(h)
17
+ */
18
+ function isSameTree ( p : TreeNode | null , q : TreeNode | null ) : boolean {
19
+ if ( p == null && q == null ) return true
20
+ if ( p == null || q == null ) return false
21
+ if ( p . val !== q . val ) return false
22
+ return isSameTree ( p . left , q . left ) && isSameTree ( p . right , q . right )
23
+ } ;
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
+ * Encodes a tree to a single string.
17
+ */
18
+ function serialize ( root : TreeNode | null ) : string {
19
+ /*
20
+ Time complexity: O(n)
21
+ Space complexity: O(n)
22
+ */
23
+ const strArray = [ ]
24
+ const dfs = ( node : TreeNode ) : void => {
25
+ if ( node == null ) {
26
+ strArray . push ( 'null' )
27
+ return
28
+ }
29
+ strArray . push ( node . val )
30
+ dfs ( node . left )
31
+ dfs ( node . right )
32
+ }
33
+ dfs ( root )
34
+ return strArray . join ( ',' )
35
+ } ;
36
+
37
+ /*
38
+ * Decodes your encoded data to tree.
39
+ */
40
+ function deserialize ( data : string ) : TreeNode | null {
41
+ /*
42
+ Time complexity: O(n)
43
+ Space complexity: O(n)
44
+ */
45
+ const values = data . split ( ',' )
46
+ let idx = 0
47
+ const dfs = ( ) : TreeNode | null => {
48
+ if ( idx >= values . length || values [ idx ] == 'null' ) {
49
+ idx ++
50
+ return null
51
+ }
52
+ const node = new TreeNode ( + values [ idx ] )
53
+ idx ++
54
+ node . left = dfs ( )
55
+ node . right = dfs ( )
56
+ return node
57
+ }
58
+
59
+ return dfs ( )
60
+ } ;
61
+
62
+
63
+ /**
64
+ * Your functions will be called as such:
65
+ * deserialize(serialize(root));
66
+ */
67
+
You can’t perform that action at this time.
0 commit comments