Advertisement

Diameter of Binary Tree - LeetCode 543 Solution

Diameter of Binary Tree - Complete Solution Guide

Diameter of Binary Tree is LeetCode problem 543, a Easy level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

Given the root of a binary tree, return the length of the diameter of the tree . The diameter of a binary tree is the length of the longest path between any two nodes in a tree. This path may or may not pass through the root . The length of a path between two nodes is represented by the number of edges between them. Example 1: Input: root = [1,2,3,4,5] Output: 3 Explanation: 3 is the length of the path [4,2,1,3] or [5,2,1,3]. Example 2: Input: root = [1,2] Output: 1 Constraints: The number of no

Detailed Explanation

The problem asks us to find the diameter of a binary tree. The diameter is defined as the longest path between any two nodes in the tree. This path may or may not pass through the root node. The length of a path is the number of edges between the nodes. The input is the root node of the binary tree, and the output is the length of the diameter.

Solution Approach

The solution utilizes a recursive Depth-First Search (DFS) approach. The core idea is to calculate the height of the left and right subtrees for each node. During this height calculation, we also update the diameter of the tree. The diameter at a node is the sum of the left subtree's height and the right subtree's height. We maintain a global variable (or a member variable in a class) to keep track of the maximum diameter encountered so far. The DFS function returns the height of the subtree rooted at the current node.

Step-by-Step Algorithm

  1. Step 1: Initialize a variable (e.g., `diameter` or `maxDiameter`) to store the maximum diameter found so far. Initialize it to 0.
  2. Step 2: Define a recursive helper function (e.g., `depth` or `maxDepth`) that takes a node as input.
  3. Step 3: Base case: If the node is NULL (or None), return 0 (height of an empty tree is 0).
  4. Step 4: Recursively call the helper function on the left child to get the height of the left subtree (leftDepth).
  5. Step 5: Recursively call the helper function on the right child to get the height of the right subtree (rightDepth).
  6. Step 6: Update the `diameter` variable with the maximum of its current value and the sum of `leftDepth` and `rightDepth`. This step calculates the diameter that passes through the current node.
  7. Step 7: Return the height of the subtree rooted at the current node, which is 1 + max(leftDepth, rightDepth). This value is used by the parent node's calculation.
  8. Step 8: Call the helper function on the root node to start the DFS traversal.
  9. Step 9: Return the value of the `diameter` variable, which now holds the maximum diameter found in the tree.

Key Insights

  • Insight 1: The diameter of a tree at a given node can be computed by summing the depths of its left and right subtrees.
  • Insight 2: We can use Depth-First Search (DFS) to traverse the tree and calculate the depth of each subtree. While calculating the depth, we can simultaneously update the diameter.
  • Insight 3: The diameter might not pass through the root, so we need to keep track of the maximum diameter found so far during the DFS traversal.

Complexity Analysis

Time Complexity: O(n)

Space Complexity: O(h)

Topics

This problem involves: Tree, Depth-First Search, Binary Tree.

Companies

Asked at: Aurora, LinkedIn, Verkada, Visa, Wix.