File tree Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Expand file tree Collapse file tree 1 file changed +34
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Definition for a binary tree node.
3
+ * function TreeNode(val, left, right) {
4
+ * this.val = (val===undefined ? 0 : val)
5
+ * this.left = (left===undefined ? null : left)
6
+ * this.right = (right===undefined ? null : right)
7
+ * }
8
+ */
9
+ /**
10
+ * @param {TreeNode } root
11
+ * @return {number }
12
+ */
13
+ const widthOfBinaryTree = function ( root ) {
14
+ let max = 1n
15
+ const stack = [ ]
16
+ const bi = BigInt
17
+ const width = ( root , level , pos ) => {
18
+ if ( root == null ) return
19
+ if ( level >= stack . length ) stack . push ( pos )
20
+ else {
21
+ // console.log(stack)
22
+ const tmp = pos - stack [ level ] + 1n
23
+ if ( tmp > max ) max = tmp
24
+ }
25
+ width ( root . left , level + 1 , 2n * pos )
26
+ width ( root . right , level + 1 , 2n * pos + 1n )
27
+ }
28
+ width ( root , 0 , 1n )
29
+ return Number ( max )
30
+ }
31
+
32
+
33
+ // another
34
+
1
35
/**
2
36
* Definition for a binary tree node.
3
37
* function TreeNode(val) {
You can’t perform that action at this time.
0 commit comments