Reputation: 1113
I am trying to find the max depth of a binary tree and using the recursive approach. It looks like this:
public int depth(TreeNode root) {
if(root==null) return 0;
int leftVal=maxDepth(root.left);
int rightVal=maxDepth(root.right);
return 1 + Math.max(leftVal,rightVal);
}
Now if there is only one node (the root) it will return 1. But isn't the depth of that node 0 with a height of 1 since it is the root? Or does Max Depth of a tree differ from the individual node?
Upvotes: 0
Views: 270
Reputation: 40
Depth is NOT the measure of tree, but of a node of a tree.
In your case, maxDepth of a node sounds to me like the height of that node.
Consider this image:
Upvotes: 1