easy Tree Diameter
Problem Statement
Given a binary tree, find the length of its diameter. The diameter of a tree is the number of nodes on the longest path between any two leaf nodes. The diameter of a tree may or may not pass through the root.
Note: You can always assume that there are at least two leaf nodes in the given tree.
Constraints:
- n == edges.length + 1
- 1 <= n <= 104
- 0 <= ai, bi < n
- ai != bi
Try it yourself
Try solving this question here:
✅ Solution Tree Diameter
Problem Statement
Given a binary tree, find the length of its diameter. The diameter of a tree is the number of nodes on the longest path between any two leaf nodes. The diameter of a tree may or may not pass through the root.
Note: You can always assume that there are at least two leaf nodes in the given tree.
Constraints:
- n == edges.length + 1
- 1 <= n <= 104
- 0 <= ai, bi < n
- ai != bi
Solution
This problem follows the Binary Tree Path Sum pattern. We can follow the same DFS approach. There will be a few differences:
- At every step, we need to find the height of both children of the current node. For this, we will make two recursive calls similar to DFS.
- The height of the current node will be equal to the maximum of the heights of its left or right children, plus ‘1’ for the current node.
- The tree diameter at the current node will be equal to the height of the left child plus the height of the right child plus ‘1’ for the current node:
diameter = leftTreeHeight + rightTreeHeight + 1. To find the overall tree diameter, we will use a class level variable. This variable will store the maximum diameter of all the nodes visited so far, hence, eventually, it will have the final tree diameter.
Here is the visual representation of the algorithm:
Code
Here is the code for this algorithm:
// class TreeNode {
// int val;
// TreeNode left;
// TreeNode right;
// TreeNode(int x) {
// val = x;
// }
// };
public class Solution {
private int treeDiameter = 0;
public int findDiameter(TreeNode root) {
treeDiameter = 0;
calculateHeight(root);
return treeDiameter;
}
private int calculateHeight(TreeNode currentNode) {
if (currentNode == null) return 0;
int leftTreeHeight = calculateHeight(currentNode.left);
int rightTreeHeight = calculateHeight(currentNode.right);
// if the current node doesn't have a left or right subtree, we can't have
// a path passing through it, since we need a leaf node on each side
if (leftTreeHeight != 0 && rightTreeHeight != 0) {
// diameter at the current node will be equal to the height of left subtree +
// the height of right sub-trees + '1' for the current node
int diameter = leftTreeHeight + rightTreeHeight + 1;
// update the global tree diameter
treeDiameter = Math.max(treeDiameter, diameter);
}
// height of the current node will be equal to the maximum of the heights of
// left or right subtrees plus '1' for the current node
return Math.max(leftTreeHeight, rightTreeHeight) + 1;
}
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.right = new TreeNode(3);
root.left.left = new TreeNode(4);
root.right.left = new TreeNode(5);
root.right.right = new TreeNode(6);
Solution sol = new Solution();
System.out.println("Tree Diameter: " + sol.findDiameter(root));
root.left.left = null;
root.right.left.left = new TreeNode(7);
root.right.left.right = new TreeNode(8);
root.right.right.left = new TreeNode(9);
root.right.left.right.left = new TreeNode(10);
root.right.right.left.left = new TreeNode(11);
System.out.println("Tree Diameter: " + sol.findDiameter(root));
}
}
Time Complexity
The time complexity of the above algorithm is
Space Complexity
The space complexity of the above algorithm will be
🤖 Don't fully get this? Learn it with Claude
Stuck on Tree Diameter? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Tree Diameter** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Tree Diameter** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Tree Diameter**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Tree Diameter**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.