Diameter of Binary Tree

Asked atNvidia
1Give yourself 5 minutes
2Answer out loud, not in your head
3Then compare with the answer below

The problem

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 the tree. It may or may not pass through the root.

Input : root = [1, 2, 3, 4, 5] Output : 3 Explanation : The path length between node 4 and 3 is of length 3. There are other ways to reach the solution.

Input : root = [1, 2, 3, null, 4, null, 5] Output : 4 Explanation : The path length between node 4 and 5 is of length 4.

Input : root = [5, 1, 2, 8, 3, null, 5, null, 4]

  • 1 <= Number of Nodes <= 104
  • -100 <= Node.val <= 100

cpp

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int data;
 *     TreeNode *left;
 *     TreeNode *right;
 *      TreeNode(int val) : data(val) , left(nullptr) , right(nullptr) {}
 * };
 **/

class Solution {
public:
    int diameterOfBinaryTree(TreeNode* root) {
        //your code goes here
    }
};

java

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int data;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int val) { data = val; left = null, right = null }
 * }
 **/

class Solution {
    public int diameterOfBinaryTree(TreeNode root) {
        //your code goes here
    }
}

python

# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.data = val
#         self.left = left
#         self.right = right

class Solution:
    def diameterOfBinaryTree(self, root):
        #your code goes here

javascript

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *      constructor(val = 0, left = null, right = null){
 *          this.data = val;
 *          this.left = null;
 *          this.right = null;
 *      }
 * }
 **/

class Solution {
    diameterOfBinaryTree(root) {
        //your code goes here
    }
}

csharp

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     public int val;
 *     public TreeNode left;
 *     public TreeNode right;
 *     public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
public class Solution {
    public int DiameterOfBinaryTree(TreeNode root) {
        //your code goes here
    }
}

go

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */

func diameterOfBinaryTree(root *TreeNode) int {
	//your code goes here
}
Solve on LeetCode →
Stuck? Show a way to structure it+
  1. 01State the contract and the key invariant for Diameter of Binary Tree
  2. 02Return subtree heights
  3. 03Obtain left and right heights
  4. 04Update best with their sum
  5. 05Return one plus maximum height
  6. 06Validate the result against boundary cases and state O(n) time and O(h) stack space

Reference answer

Then expect these follow-ups

  • Which invariant proves the Diameter of Binary Tree approach is correct?

    Tests: correctness reasoning

  • What edge case would you test first?

    Tests: follow-up reasoning

Free to read · better with Enzo

Practice this out loud with Enzo

Enzo runs it as a mock interview, pushes back with follow-ups, and grades you on the rubric.

Next question