Solution1
If we think about it, there may be three conditions for the largest diameter of a binary tree.
- The largest diamater exists in the left subtree of the root.
- The largest diameter exits in the right subtree of the root.
- The largest diameter exists and contains the root, that is, the corresponding path of the largest diameter extends from left subtree, including root, to right subtree.
And considering the property of a binary tree, these conditions can be applied to every node in the tree. Thus we can write a helper function to deal with this question. For every node, we will return 3 values to its parent: left subtree's max depth, right subtree's max length, and max diameter of current tree rooted at this node.
For each tree node, max left depth, max right depth, max diameter could be calculated as following respectively:
- max_left_depth = max(left_subtree's max_left depth, left subtree's max_right_depth) + 1
- max_right_depth = max(right_subtree's max_left depth, right subtree's max_right_depth) + 1
- max_diameter = max(left_subtree's max_diameter, right_subtree's max_diameter, max_left_subtree_depth + max_right_subtree_depth + 1)
As a result, for the root node, we just need to return the 3rd value we get since this records the maximum diameter for the whole tree. However, since the actual definition of the diameter is the number of edges, we simply deduct 1 from the value we get, which is the number of nodes in the longest diameter.
Suppose there are n nodes in the tree, this algorithm runs in O(logn)
time and O(1)
space without considering the recursion stack.
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int diameterOfBinaryTree(TreeNode root) {
if (root == null) {
return 0;
}
int ret[] = diameterHelper(root);
return ret[2] - 1;
}
private int[] diameterHelper(TreeNode curr) {
if (curr == null) return new int[]{0, 0, 0};
// [max depth of left subtree, max depth of right subtree, max diameter of left-curr-right subtree]
int[] ret = new int[3];
int[] leftRet = diameterHelper(curr.left);
int[] rightRet = diameterHelper(curr.right);
ret[0] = Math.max(leftRet[0], leftRet[1]) + 1;
ret[1] = Math.max(rightRet[0], rightRet[1]) + 1;
int maxDiameter = Math.max(leftRet[0], leftRet[1]) + Math.max(rightRet[0], rightRet[1]) + 1;
maxDiameter = Math.max(maxDiameter, leftRet[2]);
maxDiameter = Math.max(maxDiameter, rightRet[2]);
ret[2] = maxDiameter;
return ret;
}
}
Solution2
However, from the above solution, it can be seen that the actual longest diameter for every node is just the max_left_depth + max_right_depth. Hence we could use a global max value to keep track of the largest diameter to make the code clean.
class Solution {
int max = 0;
public int diameterOfBinaryTree(TreeNode root) {
diameterHelper(root);
return max;
}
private int diameterHelper(TreeNode root) {
if (root == null) return 0;
int leftDep = diameterHelper(root.left);
int rightDep = diameterHelper(root.right);
max = Math.max(max, leftDep + rightDep);
return Math.max(leftDep, rightDep) + 1;
}
}