-
Notifications
You must be signed in to change notification settings - Fork 161
/
maximum-width-of-binary-tree.md
68 lines (49 loc) · 1.62 KB
/
maximum-width-of-binary-tree.md
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
<p>Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels. The binary tree has the same structure as a <b>full binary tree</b>, but some nodes are null.</p>
<p>The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level, where the <code>null</code> nodes between the end-nodes are also counted into the length calculation.</p>
<p><b>Example 1:</b></p>
<pre>
<b>Input:</b>
1
/ \
3 2
/ \ \
5 3 9
<b>Output:</b> 4
<b>Explanation:</b> The maximum width existing in the third level with the length 4 (5,3,null,9).
</pre>
<p><b>Example 2:</b></p>
<pre>
<b>Input:</b>
1
/
3
/ \
5 3
<b>Output:</b> 2
<b>Explanation:</b> The maximum width existing in the third level with the length 2 (5,3).
</pre>
<p><b>Example 3:</b></p>
<pre>
<b>Input:</b>
1
/ \
3 2
/
5
<b>Output:</b> 2
<b>Explanation:</b> The maximum width existing in the second level with the length 2 (3,2).
</pre>
<p><b>Example 4:</b></p>
<pre>
<b>Input:</b>
1
/ \
3 2
/ \
5 9
/ \
6 7
<b>Output:</b> 8
<b>Explanation:</b>The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
</pre>
<p><b>Note:</b> Answer will in the range of 32-bit signed integer.</p>