二叉树中特定节点的高度

Height of particular node in a binary tree

本文关键字:高度 节点 二叉树      更新时间:2023-10-16
int finddepth(Node *node,int key)
{
 if(node==NULL)
  return 0;
  if(node->data==key)
  return 1;
  return max(depth(node->left),depth(node->right));
}

我只想计算一个特定节点的高度或深度。如何增加深度。我知道这个程序总是返回1

我认为没有必要让你变得复杂,你可以尝试以下代码

     int finddepth(Node* root)
     {
      if(root==NULL)
       return 0;
      int l=finddepth(root->left);
      int r=finddepth(root->right);
      if(l>r)
        return l+1;
      return r+1;
      }