Advertisement
1988coder

662. Maximum Width of Binary Tree

Feb 10th, 2019
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.03 KB | None | 0 0
  1.  
  2. // LeetCode URL: https://leetcode.com/problems/maximum-width-of-binary-tree/
  3. import java.util.HashMap;
  4. import java.util.LinkedList;
  5. import java.util.Queue;
  6.  
  7. // Definition for a binary tree node.
  8. class TreeNode {
  9.     int val;
  10.     TreeNode left;
  11.     TreeNode right;
  12.  
  13.     TreeNode(int x) {
  14.         val = x;
  15.     }
  16. }
  17.  
  18. /**
  19.  * DFS
  20.  *
  21.  * We know that a binary tree can be represented by an array (assume the root
  22.  * begins from the position with index 1 in the array). If the index of a node
  23.  * is i, the indices of its two children are 2*i and 2*i + 1. The idea is to use
  24.  * two arrays (start[] and end[]) to record the the indices of the leftmost node
  25.  * and rightmost node in each level, respectively. For each level of the tree,
  26.  * the width is end[level] - start[level] + 1. Then, we just need to find the
  27.  * maximum width.
  28.  *
  29.  * Time Complexity: O(N)
  30.  *
  31.  * Space Complexoty: O(Hieght of the tree) = O(N) in worst case (Recursion
  32.  * Stack)
  33.  *
  34.  * N = NUmber of nodes in the tree.
  35.  */
  36. class Solution1 {
  37.     public int widthOfBinaryTree(TreeNode root) {
  38.         if (root == null) {
  39.             return 0;
  40.         }
  41.         int[] result = new int[1];
  42.         dfsHelper(root, 0, 1, new HashMap<>(), result);
  43.         return result[0];
  44.     }
  45.  
  46.     private void dfsHelper(TreeNode node, int depth, int pos, HashMap<Integer, Integer> map, int[] result) {
  47.         if (node == null) {
  48.             return;
  49.         }
  50.         if (!map.containsKey(depth)) {
  51.             map.put(depth, pos);
  52.         }
  53.  
  54.         result[0] = Math.max(result[0], pos - map.get(depth) + 1);
  55.         dfsHelper(node.left, depth + 1, 2 * pos, map, result);
  56.         dfsHelper(node.right, depth + 1, 2 * pos + 1, map, result);
  57.     }
  58. }
  59.  
  60. /**
  61.  * BFS
  62.  *
  63.  * Refer above solution for explaination.
  64.  *
  65.  * Time Complexity: O(N)
  66.  *
  67.  * Space Complexoty: O(Hieght of the tree) = O(N) in worst case (Queue size)
  68.  *
  69.  * N = NUmber of nodes in the tree.
  70.  */
  71. class Solution2 {
  72.     class Node {
  73.         TreeNode node;
  74.         int pos;
  75.  
  76.         Node(TreeNode n, int p) {
  77.             node = n;
  78.             pos = p;
  79.         }
  80.     }
  81.  
  82.     public int widthOfBinaryTree(TreeNode root) {
  83.         if (root == null) {
  84.             return 0;
  85.         }
  86.  
  87.         Queue<Node> queue = new LinkedList<>();
  88.         queue.offer(new Node(root, 1));
  89.         int result = 0;
  90.  
  91.         while (!queue.isEmpty()) {
  92.             int queueSize = queue.size();
  93.             Node leftNode = null;
  94.             for (int i = 0; i < queueSize; i++) {
  95.                 Node cur = queue.poll();
  96.                 if (cur.node.left != null) {
  97.                     queue.offer(new Node(cur.node.left, cur.pos * 2));
  98.                 }
  99.                 if (cur.node.right != null) {
  100.                     queue.offer(new Node(cur.node.right, cur.pos * 2 + 1));
  101.                 }
  102.                 if (leftNode == null) {
  103.                     leftNode = cur;
  104.                 }
  105.  
  106.                 result = Math.max(result, cur.pos - leftNode.pos + 1);
  107.             }
  108.         }
  109.  
  110.         return result;
  111.     }
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement