在二叉树中找到最低的叶子

Find the lowest leaf in a binary tree

本文关键字:叶子 二叉树      更新时间:2023-10-16

我正在尝试比较所有叶以返回树的最低值,我没有主函数,只有一个插入值的脚本,所以很遗憾我无法调试它。

tpNoArvore * findLowest(tpNoArvore * pNo){
   tpNoArvore * left;
   tpNoArvore * right;
   tpNoArvore * res;
   if (!pNo) return NULL; /* if */
   left = findLowest(pNo->pNoL);
   right = findLowest(pNo->pNoR);
   if(isLeaf(pNo))
      return pNo;
   }  /* if */
   if(!left){
      return right;
   } /* if */
   if(!right){
      return left;
   } /* if */

    return (left->Valor < right->Valor) ? left : right ;
} 

所以,基本上,我在这里试图实现的是比较每个节点的两侧,找到最低的。

您的代码返回指针似乎很奇怪。我希望有这样的东西:

// Assume valor is int
int findLowest(tpNoArvore * pNo){
   if (!pNo) exit(1); /* fatal error */
   // If this is a leaf just return its value
   if(isLeaf(pNo)) return pNo->Valor;
   // Not a leaf
   // Find the lowest value in left part of tree
   int leftValor = findLowest(pNo->pNoL);
   // Find the lowest value in right part of tree
   int rightValor = findLowest(pNo->pNoR);
   // Return the lowest of leftValue ans rightValue
   return (leftValor < rightValor) ? leftValor : rightValor ;
} 

但也许我误解了你的问题。