Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 递归函数,用于查找给定节点的树中最右边的叶_C++_Algorithm_Recursion_Tree - Fatal编程技术网

C++ 递归函数,用于查找给定节点的树中最右边的叶

C++ 递归函数,用于查找给定节点的树中最右边的叶,c++,algorithm,recursion,tree,C++,Algorithm,Recursion,Tree,这段代码可以递归地找到最右边的叶子吗。多谢各位 Node* findRightMostLeaf(Node* curNode){ if (curNode== NULL) return null; if (curNode->rchild!= NULL) return findRightMostLeaf(curNode->rchild); if (curNode->lchild!= NULL) return findRightMostLeaf(curN

这段代码可以递归地找到最右边的叶子吗。多谢各位

 Node* findRightMostLeaf(Node* curNode){
 if (curNode== NULL) 
     return null;
 if (curNode->rchild!= NULL)
   return findRightMostLeaf(curNode->rchild);
 if (curNode->lchild!= NULL)
   return findRightMostLeaf(curNode->lchild);
else return curNode; } 

假设构造的树是一个二叉搜索树。在这种情况下,您的解决方案应该有效。 您可以在这里检查其他迭代解

我建议你在一张纸上画一棵小树,从根节点(或任何其他节点)开始,看看这段代码带你去哪里,它不长,你可以跟着它走。既然你已经编写了函数,为什么不试试呢?如果它不符合您的期望,请提供一个最低限度的工作示例。不,此代码将不起作用。即使您提供了
节点
的定义,
curNode
是一个指针,因此您必须使用
->
而不是
,并且
null
是未定义的。在编写此函数之前,我确实绘制了它,但由于我不熟悉递归,我正在努力确保它是正确的,如果没有一些建议,我哪里做错了?你的模型中的leaf是什么?它与节点不同吗?该网站上的示例以
#include/using namespace std。对于那些学习C++的人,我建议避免使用这个网站——它教我们如何编写坏代码,与专业编程无关。实际上,我的主要精力集中在提供一个简单的可替代的算法示例,这是容易理解和理解的。只包含外部链接而没有解释的答案不是很有用。当链接消失时,它们就完全无用了。