Diameter of a Binary Tree

    by nikoo28

    You are provided with a binary tree, return the length of the diameter. The diameter of the binary tree is defined as the length of the longest path between any two nodes in the tree.

    diameter_sample
    Diameter of a binary tree 7 through root

    In the above example the diameter of the binary tree is 7.

    Problem Statement:

    We can easily find out the distance between two nodes in a binary tree. In the given example, the distance between node 5 and 8 is 4. The distance between node 2 and 6 is 3, the distance between node 8 and 0 is 2 and so on. To put the problem in simple words, the diameter of a binary tree is the maximum of all such lengths you can find. If you try to calculate all the distances between every possible node, you would see that longest path between 2 nodes is 7. (formed by nodes 6 and 9).

    Fig showing different path lengths and diameter
    Fig: Different path lengths and diameter
    Brute Force Method:

    Just as you look at the problem, one solution seems very obvious.

    • Take up a node. Find the distance to each of the node and keep a track of the maximum value you find.
    • Start with another node, and again repeat the same process.
    • Do this for all the nodes in the tree.
    • Once we have completed this process for all the nodes, we will have maximum possible distance between two nodes.
    • This would be the diameter of the binary tree.

    But this method would only work efficiently for small trees having 5-6 nodes. Once the tree begins to grow in size, you realize that we are wasting a lot of computation in calculating unnecessary paths making the time complexity to be O(n^2).

    Recursive Method:

    A very popular way to solve this problem is using recursion. The way recursion works is that every left and right child of a node is a tree in itself. So the maximum possible path can be found in the left sub-tree or in the right sub-tree. It may also be possible that the path includes the root node.

    So, if the longest path will include the root node, then the longest path must be the depth(root->right) + depth (root->left). If not, then we would find the diameter either in the left sub-tree or in the right sub-tree. The algorithm for a recursive method looks like:

    • Find the diameter of the left sub-tree.
    • Find the diameter of the right sub-tree.
    • The diameter may pass through the root so add 1 to the length.
    • The greatest value among these 3 values would be the diameter of the binary tree.
    Code:
    int max = 0;
    private int diameter(TreeNode root) {
        max = 1;
        depth(root);
        return max - 1;
    }
    
    private int depth(TreeNode node)
    {
        if(node == null)
        {
            return 0;
        }
    
        int leftDepth = depth(root.left);
        int rightDepth = depth(root.right);
    
        // update the answer, because diameter of a  
        // tree is nothing but maximum value of  
        // (left_depth + right_depth + 1) for each node  
        max = Math.max(max, leftDepth + rightDepth + 1);
        return 1 + Math.max(leftDepth, rightDepth);        
    }
    Code language: Java (java)

    This method works perfectly and has a time complexity of O(n). But it occupies stack space in memory and can be harder to debug because of the recursion involved.

    Iterative Method:

    To this point, we are clear about one thing. To find the diameter of a tree, we need to process the left and right sub-tree first. This implies that using a post-order traversal may be helpful here. It makes sure that we traverse the left and right sub-tree before moving to the root.

    Here is an image showing maximum depth of each of the nodes in the tree. The diameter of the tree is the maximum value of (left_depth + right_depth) among each of the nodes.

    figure showing maximum depth at each node
    Fig: Showing maximum depth at each node.
    Diameter is the maximum of (left_depth + right_depth) = 7

    We use stacks to perform a post order traversal of the binary tree. A hash-map can be used to maintain the maximum depth at each of the nodes in the tree.

    Code:
      public int diameterOfBinaryTree(TreeNode root) {
    
        Map<TreeNode, Integer> map = new HashMap<>();
        Stack<TreeNode> stack = new Stack<>();
        int diameter = 0;
    
        if (root != null)
          stack.push(root);
    
        while (!stack.isEmpty()) {
          TreeNode node = stack.peek();
    
          // Fill up stack to perform post-order traversal
          if (node.left != null && !map.containsKey(node.left)) {
            stack.push(node.left);
          } else if (node.right != null && !map.containsKey(node.right)) {
            stack.push(node.right);
          } else {
    
            // Process the root, once left and right sub-tree have been processed
            stack.pop();
            int leftDepth = map.getOrDefault(node.left, 0);
            int rightDepth = map.getOrDefault(node.right, 0);
    
            // Put the max depth at a node in the map
            map.put(node, 1 + Math.max(leftDepth, rightDepth));
    
            // Update the max diameter found so far
            diameter = Math.max(diameter, leftDepth + rightDepth);
          }
        }
        return diameter;
      }
    Code language: Java (java)

    Time Complexity: O(n)
    Space Complexity: O(n)

    You can find a working solution here.
    The code and test cases are also available on Github.
    A similar problem can be found on Leetcode.

    Video Explanation:
    YouTube player
    4 comments
    1 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Maximum sum contiguous sub-array

    by nikoo28
    5 minutes read

    You are provided with an array of integers. We need to find the sub-array with the maximum sum such that all the elements are contiguous. Example:Input: [-2, -5, 6, -2, -3, 1, 5, -6]Output: 7Explanation: The sub-array with maximum sum is [6, -2, -3, 1, 5] Problem Statement: You are provided with an array of integers. These elements could be all positive, all negative or a combination of both. A sub-array is a smaller array formed using the elements of the original array. The condition for this problem is that …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Linked List

    Merge two sorted Linked Lists

    by nikoo28
    4 minutes read

    You are given 2 sorted linked lists with a head pointer. Find a way to merge these 2 lists and return a single sorted list. Try to solve the problem without using any extra space. Example 1:List 1: 1 -> 2 -> 4List 2: 1 -> 3 -> 4Output: 1 -> 1 -> 2 -> 3 -> 4 -> 4 Problem statement To read about Linked Lists, refer to the post “What is a Linked List“? For our current problem statement, we are given two list heads or we refer …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Single non-repeating number

    by nikoo28
    4 minutes read

    Let us suppose you are given an array of integers. The special thing about this array is that all numbers are repeated twice except one. Find the single non-repeating number. Example 1:Input: [2, 1, 1]Output: 2 Example 2:Input: [4, 5, 5, 2, 2]Output: 4 Problem statement: You are given an array of all positive integers. All the integers are repeated exactly twice except one. We need to find that number with a linear time complexity and using the minimum space possible. Brute Force Method: When tackling such problems, it is …

    1 FacebookTwitterLinkedinWhatsappEmail
  • Strings

    Longest Palindromic Substring

    by nikoo28
    4 minutes read

    Let us suppose that we are given a string ‘str’. You need to find out the longest palindrome as a substring. Note that the elements of the string should be contagious. Example 1:Input: “aaebabad”Output: “bab”Note: “aba” is also a valid answer Example 2:Input: “cbeereed”Output: “eeree” Terminology: Palindrome: A palindrome is a type of string that reads the same when reading left to right and right to left. An example of a palindrome can be “level”, “racecar”, “kayak” etc. Sub-string: A string formed formed using the characters of the string that …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Question: Given an array A, find the sum of maximum sum of two non-overlapping subarrays, with lengths L and M.In other words, return the largest V for which V = (A[i] + A[i+1] …. A[i+L-1]) + (A[j] + A[j+1] …. A[j+M-1])Input: A = {3, 8, 1, 3, 2, 1, 8, 9, 0}; L = 3, M = 2Output: 29 The problem statement is quite clear. We need to find 2 contiguous sub-arrays from an array Add the elements of both these arrays The sum should be the maximum possible sum …

    0 FacebookTwitterLinkedinWhatsappEmail
  • Arrays

    Array Nesting

    by nikoo28
    4 minutes read

    Question: Given a zero-indexed array ‘A’ of length ‘N’ which contains all integers from 0 to N-1. Find and return the longest length of set S, where S[i] = {A[i], A[A[i]], A[A[A[i]]], …} subjected to a particular condition. Suppose the first element in S starts with the selection of element A[i] of index = i, the next element in S should be A[A[i]], and then A[A[A[i]]],.. and so on. By that analogy, we stop adding elements as soon as we get a duplicate. Input: A = {5, 4, 0, 3, …

    0 FacebookTwitterLinkedinWhatsappEmail

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More