user081608
user081608

Reputation: 1113

Max Depth of binary tree with size one

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

Answers (1)

Leo Mingo
Leo Mingo

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.

  • The depth of a node is the number of edges from the root to the node.
  • The height of a node is the number of edges from the node to the deepest leaf.
  • The height of a tree is a height of the root.

Consider this image:

Depth and Height

Upvotes: 1

Related Questions