662-Maximum Width of Binary Tree
Description
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 full binary tree, but some nodes are null.
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 null nodes between the end-nodes are also counted into the length calculation.
Example 1:
Input:
1
/ \
3 2
/ \ \
5 3 9
Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
Example 2:
Input:
1
/
3
/ \
5 3
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).
Example 3:
Input:
1
/ \
3 2
/
5
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).
Example 4:
Input:
1
/ \
3 2
/ \
5 9
/ \
6 7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).
Note: Answer will in the range of 32-bit signed integer.
問題描述
給定一個二叉樹, 返回其最大寬度。一個樹的寬度為其所有層級中的最大寬度。給定的二叉樹與滿二叉樹結構相同, 但是其中的一些節點為空節點。
一個層級的寬度被定義為該層中的端點之間的距離(最左邊及最右邊的非空節點, 端點間的空節點也要包括在距離計算中)
問題分析
關鍵是為每層的節點標記序號, 序號的產生規則為左分支為2i, 右分支為2i + 1, i初始化為1
遞迴的解法注意利用高度
解法1
class Solution {
private int max = 0;
public int widthOfBinaryTree(TreeNode root) {
List<Integer> startIndexOfEachLevel = new ArrayList<>();
helper(root, 1, 1, startIndexOfEachLevel);
return max;
}
public void helper(TreeNode node, int level, int index, List<Integer> startIndexOfEachLevel) {
if(node == null) return;
if(level > startIndexOfEachLevel.size()) startIndexOfEachLevel.add(index);
max = Math.max(max, index - startIndexOfEachLevel.get(level - 1) + 1);
helper(node.left, level + 1, index * 2, startIndexOfEachLevel);
helper(node.right, level + 1, index * 2 + 1, startIndexOfEachLevel);
}
}
解法2
class Solution {
private int max = 0;
public int widthOfBinaryTree(TreeNode root) {
List<Integer> startIndexOfEachLevel = new ArrayList<>();
helper(root, 1, 1, startIndexOfEachLevel);
return max;
}
public void helper(TreeNode node, int level, int index, List<Integer> startIndexOfEachLevel) {
if(node == null) return;
if(level > startIndexOfEachLevel.size()) startIndexOfEachLevel.add(index);
max = Math.max(max, index - startIndexOfEachLevel.get(level - 1) + 1);
helper(node.left, level + 1, index * 2, startIndexOfEachLevel);
helper(node.right, level + 1, index * 2 + 1, startIndexOfEachLevel);
}
}
相關文章
- 662. Maximum Width of Binary Tree
- Traversals of binary tree
- Leetcode Binary Tree PathsLeetCode
- [LintCode] Binary Tree Level Order
- 545. Boundary of Binary Tree
- 257. Binary Tree Paths
- Construct String from Binary TreeStruct
- [LintCode] Check Full Binary Tree
- 257-Binary Tree Paths
- 543-Diameter of Binary Tree
- 563-Binary Tree Tilt
- 655-Print Binary Tree
- 654-Maximum Binary Tree
- 814-Binary Tree Pruning
- 110-Balanced Binary Tree
- 226-Invert Binary Tree
- 104. Maximum Depth of Binary Tree
- LeetCode 543. Diameter of Binary TreeLeetCode
- Binary Tree Level Order Traversal [LEETCODE]LeetCode
- LeetCode545.Boundary-of-Binary-TreeLeetCode
- Leetcode 226. Invert Binary TreeLeetCode
- [leetcode]binary-tree-inorder-traversalLeetCode
- [leetcode]maximum-depth-of-binary-treeLeetCode
- 173. Binary Search Tree Iterator
- [LeetCode] 226. Invert Binary TreeLeetCode
- [LeetCode] 543. Diameter of Binary TreeLeetCode
- LeetCode之Univalued Binary Tree(Kotlin)LeetCodeKotlin
- LeetCode之Binary Tree Pruning(Kotlin)LeetCodeKotlin
- 111-Minimum Depth of Binary Tree
- Binary-tree-level-order-traversal
- 104-Maximum Depth of Binary Tree
- 637-Average of Levels in Binary Tree
- 669-Trim a Binary Search Tree
- LeetCode#110.Balanced Binary Tree(Tree/Height/DFS/Recursion)LeetCode
- LeetCode 104. Maximum Depth of Binary TreeLeetCode
- LeetCode 98. Validate Binary Search TreeLeetCode
- LeetCode | 144. Binary Tree Preorder TraversalLeetCode
- LeetCode | 145. Binary Tree Postorder TraversalLeetCode