File tree 3 files changed +83
-2
lines changed
3 files changed +83
-2
lines changed Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/binary-tree-right-side-view/description/?envType=study-plan-v2&envId=leetcode-75
2
+
3
+ /**
4
+ * Definition for a binary tree node.
5
+ * class TreeNode {
6
+ * val: number
7
+ * left: TreeNode | null
8
+ * right: TreeNode | null
9
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10
+ * this.val = (val===undefined ? 0 : val)
11
+ * this.left = (left===undefined ? null : left)
12
+ * this.right = (right===undefined ? null : right)
13
+ * }
14
+ * }
15
+ */
16
+
17
+ // @ts -nocheck
18
+ const rightSideView = ( root : TreeNode | null ) : number [ ] => {
19
+ if ( root === null ) return [ ] ;
20
+
21
+ const values = [ ] ;
22
+ const queue = [ root ] ;
23
+
24
+ while ( queue . length ) {
25
+ const initialQueueLength = queue . length ;
26
+
27
+ for ( let i = 0 ; i < initialQueueLength ; i ++ ) {
28
+ const current = queue . shift ( ) ;
29
+ if ( i === initialQueueLength - 1 ) values . push ( current . val ) ;
30
+ if ( current . left ) queue . push ( current . left ) ;
31
+ if ( current . right ) queue . push ( current . right ) ;
32
+ }
33
+ }
34
+
35
+ return values ;
36
+ } ;
Original file line number Diff line number Diff line change
1
+ // https://leetcode.com/problems/maximum-level-sum-of-a-binary-tree/description/?envType=study-plan-v2&envId=leetcode-75
2
+
3
+ /**
4
+ * Definition for a binary tree node.
5
+ * class TreeNode {
6
+ * val: number
7
+ * left: TreeNode | null
8
+ * right: TreeNode | null
9
+ * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
10
+ * this.val = (val===undefined ? 0 : val)
11
+ * this.left = (left===undefined ? null : left)
12
+ * this.right = (right===undefined ? null : right)
13
+ * }
14
+ * }
15
+ */
16
+
17
+ // @ts -nocheck
18
+ const maxLevelSum = ( root : TreeNode | null ) : number => {
19
+ const queue = [ root ] ;
20
+
21
+ let level = 1 ,
22
+ maxSum = root . val ,
23
+ maxLevel = 1 ;
24
+
25
+ while ( queue . length ) {
26
+ const initialQueueLength = queue . length ;
27
+ let tempSum = 0 ;
28
+
29
+ for ( let i = 0 ; i < initialQueueLength ; i ++ ) {
30
+ const current = queue . shift ( ) ;
31
+ tempSum += current . val ;
32
+ if ( current . left ) queue . push ( current . left ) ;
33
+ if ( current . right ) queue . push ( current . right ) ;
34
+ }
35
+
36
+ if ( tempSum > maxSum ) {
37
+ maxLevel = level ;
38
+ maxSum = tempSum ;
39
+ }
40
+
41
+ level ++ ;
42
+ }
43
+
44
+ return maxLevel ;
45
+ } ;
Original file line number Diff line number Diff line change 69
69
70
70
### Binary Tree - BFS
71
71
72
- - [ ] 39 . Binary Tree Right Side View
73
- - [ ] 40 . Maximum Level Sum of a Binary Tree
72
+ - [X ] 39 . Binary Tree Right Side View
73
+ - [X ] 40 . Maximum Level Sum of a Binary Tree
74
74
75
75
### Binary Search Tree
76
76
You can’t perform that action at this time.
0 commit comments