Algo-Sim › Binary Trees & BST
Diameter of Binary Tree
Easy · Binary Trees & BST · step through it in Java, Python and C++ on your own input
The longest path bends at some node: track left height + right height at every node while returning heights.
Problem
The diameter of a binary tree is the number of edges on the longest path between any two nodes; the path does not have to pass through the root. Return the diameter.
Constraints
- 1 ≤ number of nodes ≤ 10⁴
- -100 ≤ Node.val ≤ 100
- Length is counted in edges, not nodes
Edge cases to test
- One node:
0edges - The longest path skips the root
- A chain
Follow-up questions
- Edges or nodes?
- This counts edges, so the answer is left height + right height at the best node. Check which one the interviewer means.
- What about a general tree or graph?
- For a tree: BFS to the farthest node, then BFS again; the second distance is the diameter: O(n).
Solution
Java
private int best = 0;
public int diameterOfBinaryTree(TreeNode root) {
height(root);
return best;
}
private int height(TreeNode node) {
if (node == null) return 0;
int left = height(node.left);
int right = height(node.right);
best = Math.max(best, left + right);
return 1 + Math.max(left, right);
}Python
def diameter_of_binary_tree(root):
best = 0
def height(node):
nonlocal best
if not node:
return 0
left = height(node.left)
right = height(node.right)
best = max(best, left + right)
return 1 + max(left, right)
height(root)
return bestC++
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
int best = 0;
int diameterOfBinaryTree(TreeNode *root) {
height(root);
return best;
}
int height(TreeNode *node) {
if (node == nullptr)
return 0;
int left = height(node->left);
int right = height(node->right);
best = std::max(best, left + right);
return 1 + std::max(left, right);
}
};
More Binary Trees & BST problems
- Preorder Traversal (Recursive) (Easy)
- Inorder Traversal (Recursive) (Easy)
- Postorder Traversal (Recursive) (Easy)
- Preorder Traversal (Iterative) (Easy)
- Inorder Traversal (Iterative) (Easy)
- Postorder Traversal (Iterative) (Easy)
- Binary Tree Level Order Traversal (Medium)
- Binary Tree Zigzag Level Order Traversal (Medium)
- Binary Tree Right Side View (Medium)
- Vertical Order Traversal of a Binary Tree (Hard)
- Maximum Depth of Binary Tree (Easy)
- Balanced Binary Tree (Easy)
- Invert Binary Tree (Easy)
- Flatten Binary Tree to Linked List (Medium)
- Same Tree (Easy)
- Symmetric Tree (Easy)
- Subtree of Another Tree (Easy)
- Path Sum (Easy)
- Path Sum II (Medium)
- Path Sum III (Medium)
- Count Good Nodes in Binary Tree (Medium)
- Lowest Common Ancestor of a Binary Tree (Medium)
- Kth Ancestor of a Tree Node (Hard)
- All Nodes Distance K in Binary Tree (Medium)
- Validate Binary Search Tree (Medium)
- Search in a Binary Search Tree (Easy)
- Insert into a Binary Search Tree (Medium)
- Delete Node in a BST (Medium)
- Kth Smallest Element in a BST (Medium)
- Binary Search Tree Iterator (Medium)
- Convert Sorted Array to Binary Search Tree (Easy)
- AVL Tree Insert (Hard)
- Construct Binary Tree from Preorder and Inorder Traversal (Medium)
- Binary Tree Maximum Path Sum (Hard)
- Serialize and Deserialize Binary Tree (Hard)