项目作者: eMahtab

项目描述 :
Calculate the maximum width of Binary Tree
高级语言:
项目地址: git://github.com/eMahtab/maximum-width-of-binary-tree.git
创建时间: 2020-02-02T16:42:34Z
项目社区:https://github.com/eMahtab/maximum-width-of-binary-tree

开源协议:

下载


Maximum Width of Binary Tree

https://leetcode.com/problems/maximum-width-of-binary-tree

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.

  1. Example 1:
  2. Input:
  3. 1
  4. / \
  5. 3 2
  6. / \ \
  7. 5 3 9
  8. Output: 4
  9. Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).
  10. Example 2:
  11. Input:
  12. 1
  13. /
  14. 3
  15. / \
  16. 5 3
  17. Output: 2
  18. Explanation: The maximum width existing in the third level with the length 2 (5,3).
  19. Example 3:
  20. Input:
  21. 1
  22. / \
  23. 3 2
  24. /
  25. 5
  26. Output: 2
  27. Explanation: The maximum width existing in the second level with the length 2 (3,2).
  28. Example 4:
  29. Input:
  30. 1
  31. / \
  32. 3 2
  33. / \
  34. 5 9
  35. / \
  36. 6 7
  37. Output: 8
  38. Explanation:
  39. The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).

Note: Answer will be in the range of 32-bit signed integer.

Implementation 1 : Iterative , Time : O(nodes in tree) , Space : O(nodes in tree)

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode() {}
  8. * TreeNode(int val) { this.val = val; }
  9. * TreeNode(int val, TreeNode left, TreeNode right) {
  10. * this.val = val;
  11. * this.left = left;
  12. * this.right = right;
  13. * }
  14. * }
  15. */
  16. class Solution {
  17. class Node {
  18. TreeNode treeNode;
  19. int position;
  20. Node(TreeNode treeNode, int position) {
  21. this.treeNode = treeNode; this.position = position;
  22. }
  23. }
  24. public int widthOfBinaryTree(TreeNode root) {
  25. if(root == null)
  26. return 0;
  27. Queue<Node> q = new LinkedList<>();
  28. int maxWidth = 1;
  29. q.add(new Node(root, 0));
  30. while(!q.isEmpty()) {
  31. int size = q.size();
  32. int left = 0, right = 0;
  33. for(int i = 0; i < size; i++) {
  34. Node node = q.remove();
  35. if(i == 0)
  36. left = node.position;
  37. if(i == size-1)
  38. right = node.position;
  39. if(node.treeNode.left != null)
  40. q.add(new Node(node.treeNode.left, 2 * node.position));
  41. if(node.treeNode.right != null)
  42. q.add(new Node(node.treeNode.right, 2 * node.position + 1));
  43. }
  44. int width = (right - left) + 1;
  45. maxWidth = Math.max(maxWidth, width);
  46. }
  47. return maxWidth;
  48. }
  49. }

Implementation 2 : Recursive , Time : O(nodes in tree) , Space : O(nodes in tree)

  1. /**
  2. * Definition for a binary tree node.
  3. * public class TreeNode {
  4. * int val;
  5. * TreeNode left;
  6. * TreeNode right;
  7. * TreeNode(int x) { val = x; }
  8. * }
  9. */
  10. class Solution {
  11. int maxWidth = 0;
  12. Map<Integer, Integer> leftmostNodePosition = new HashMap<>();
  13. public int widthOfBinaryTree(TreeNode root) {
  14. if(root == null)
  15. return 0;
  16. dfs(root, 0, 0);
  17. return maxWidth;
  18. }
  19. public void dfs(TreeNode root, int depth, int position) {
  20. if (root == null) return;
  21. leftmostNodePosition.putIfAbsent(depth, position);
  22. maxWidth = Math.max(maxWidth, position - leftmostNodePosition.get(depth) + 1);
  23. dfs(root.left, depth + 1, 2 * position);
  24. dfs(root.right, depth + 1, 2 * position + 1);
  25. }
  26. }

References :

https://www.youtube.com/watch?v=sm4UdCO2868