首页 > 试题广场 >

以下算法是求取一个二叉树的最大深度(从根节点出发,与最远的叶

[不定项选择题]
以下算法是求取一个二叉树的最大深度(从根节点出发,与最远的叶子节点的路径上,节点的数目)
int maxDepth(struct node* node) 

   if (node==NULL) 
       return 0; 
   else
   { 
       /* compute the depth of each subtree */
       int lDepth = maxDepth(node->left); 
       int rDepth = maxDepth(node->right); 
   
       /* use the larger one */
       if (lDepth > rDepth) 
           return X; 
       else return Y; 
   } 

请问X 和 Y应该填写以下何值,该算法能正常运算。
  • X = lDepth, Y = rDepth
  • X = lDepth + 1, Y = rDepth + 1
  • X = lDepth – 1, Y = rDepth -1
  • None of the above

这道题你会答吗?花几分钟告诉大家答案吧!