-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path662-MaximumWidthOfBinaryTree.go
More file actions
149 lines (138 loc) · 4.71 KB
/
662-MaximumWidthOfBinaryTree.go
File metadata and controls
149 lines (138 loc) · 4.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package main
// 662. Maximum Width of Binary Tree
// Given the root of a binary tree, return the maximum width of the given tree.
// The maximum width of a tree is the maximum width among all levels.
// The width of one level is defined as the length between the end-nodes (the leftmost and rightmost non-null nodes),
// where the null nodes between the end-nodes that would be present in a complete binary tree extending down to that level are also counted into the length calculation.
// It is guaranteed that the answer will in the range of a 32-bit signed integer.
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg" />
// Input: root = [1,3,2,5,3,null,9]
// Output: 4
// Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg" />
// Input: root = [1,3,2,5,null,null,9,6,null,7]
// Output: 7
// Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg" />
// Input: root = [1,3,2,5]
// Output: 2
// Explanation: The maximum width exists in the second level with length 2 (3,2).
// Constraints:
// The number of nodes in the tree is in the range [1, 3000].
// -100 <= Node.val <= 100
import "fmt"
// Definition for a binary tree node.
type TreeNode struct {
Val int
Left *TreeNode
Right *TreeNode
}
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
// dfs
func widthOfBinaryTree(root *TreeNode) int {
res := 0
lst := []int{}
var dfs func(root *TreeNode, id, depth int, lst *[]int)
dfs = func (root *TreeNode, id, depth int, lst *[]int) {
if root == nil {
return
}
if depth >= len(*lst) {
*lst = append(*lst, id)
}
if (id + 1 - (*lst)[depth]) > res {
res = id + 1 - (*lst)[depth]
}
if root.Left != nil { dfs(root.Left, id*2, depth+1, lst) }
if root.Right != nil { dfs(root.Right, id*2+1, depth+1, lst) }
}
dfs(root, 1, 0, &lst)
return res
}
// bfs
func widthOfBinaryTree1(root *TreeNode) int {
type Node struct {
id int
tree *TreeNode
}
s, h, t, res := make([]*Node, 1), 0, 1, 1
s[0] = &Node{
id: 1,
tree: root,
}
n := s[0]
for h < t {
x := s[h]
if x.tree.Left != nil {
s = append(s, &Node{
id: x.id*2,
tree: x.tree.Left,
})
t++
}
if x.tree.Right != nil {
s = append(s, &Node{
id: x.id*2+1,
tree: x.tree.Right,
})
t++
}
if x == n && h < t-1 {
w := s[t-1].id - s[h+1].id + 1
if w > res {
res = w
}
n = s[t-1]
}
h++
}
return res
}
func main() {
// Example 1:
// <img src="https://assets.leetcode.com/uploads/2021/05/03/width1-tree.jpg" />
// Input: root = [1,3,2,5,3,null,9]
// Output: 4
// Explanation: The maximum width exists in the third level with length 4 (5,3,null,9).
tree1 := &TreeNode {
1,
&TreeNode{3, &TreeNode{5, nil, nil, }, &TreeNode{3, nil, nil, }, },
&TreeNode{2, nil, &TreeNode{9, nil, nil, }, },
}
fmt.Println(widthOfBinaryTree(tree1)) // 4
// Example 2:
// <img src="https://assets.leetcode.com/uploads/2022/03/14/maximum-width-of-binary-tree-v3.jpg" />
// Input: root = [1,3,2,5,null,null,9,6,null,7]
// Output: 7
// Explanation: The maximum width exists in the fourth level with length 7 (6,null,null,null,null,null,7).
tree2 := &TreeNode {
1,
&TreeNode{3, &TreeNode{5, &TreeNode{6, nil, nil, }, nil, }, nil, },
&TreeNode{2, nil, &TreeNode{9, &TreeNode{7, nil, nil, }, nil, }, },
}
fmt.Println(widthOfBinaryTree(tree2)) // 7
// Example 3:
// <img src="https://assets.leetcode.com/uploads/2021/05/03/width3-tree.jpg" />
// Input: root = [1,3,2,5]
// Output: 2
// Explanation: The maximum width exists in the second level with length 2 (3,2).
tree3 := &TreeNode {
1,
&TreeNode{3, &TreeNode{5, nil, nil, }, nil, },
&TreeNode{2, nil, nil, },
}
fmt.Println(widthOfBinaryTree(tree3)) // 2
fmt.Println(widthOfBinaryTree1(tree1)) // 4
fmt.Println(widthOfBinaryTree1(tree2)) // 7
fmt.Println(widthOfBinaryTree1(tree3)) // 2
}