Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/34.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++_Binary Tree - Fatal编程技术网

C++ 寻找二叉树的深度

C++ 寻找二叉树的深度,c++,binary-tree,C++,Binary Tree,我试图写一些东西来确定二叉树的最大深度,但到目前为止,只得到了一个东西,它会不断返回树中的节点数,下面的另一个东西,它总是多多少少有一个。经过几个小时的调整,我真的需要一些建议 void findthedepth(nodeoftree<node>* root, int* depthtotal, int* depthcurrent){ int left = 0, right = 0; if( root == nullptr ){ *depthtota

我试图写一些东西来确定二叉树的最大深度,但到目前为止,只得到了一个东西,它会不断返回树中的节点数,下面的另一个东西,它总是多多少少有一个。经过几个小时的调整,我真的需要一些建议

void findthedepth(nodeoftree<node>* root, int* depthtotal, int* depthcurrent){

    int left = 0, right = 0;

    if( root == nullptr ){
        *depthtotal = 0;
        *depthcurrent = 0;
        return;
    }

    findthedepth(root->rightp(), depthtotal, depthcurrent);
    right = *depthcurrent;

    *depthcurrent = 0;

    findthedepth(root->leftp(), depthtotal, depthcurrent);
    left = *depthcurrent;


    if (left > right){ 
        *depthtotal += left + 1;
    }
    else { 
        *depthtotal += right + 1;
    }
}
void findthedepth(nodeoftree*root,int*depthtotal,int*depthcurrent){
int left=0,right=0;
if(root==nullptr){
*深度合计=0;
*深度电流=0;
返回;
}
findthedepth(root->rightp(),depthtotal,depthcurrent);
右=*深度电流;
*深度电流=0;
findthedepth(root->leftp(),depthtotal,depthcurrent);
左=*深度电流;
如果(左>右){
*depthtotal+=左+1;
}
否则{
*深度合计+=右侧+1;
}
}

有两种情况需要考虑:

  • 空树的深度为零
  • 非空树的深度比其两个子树的深度高一级,因此它的深度
    1+max(depth\u left,depth\u right)
如果我们用C++写出来:

int depth(nodeoftree<node>* root) {
    if (root == nullptr)
        return 0;

    int depth_left = depth(node->leftp());
    int depth_right = depth(node->rightp());
    return 1 + max(depth_left, depth_right);
}
int深度(nodeoftree*root){
if(root==nullptr)
返回0;
int depth_left=depth(节点->leftp());
int depth_right=depth(node->rightp());
返回1+最大值(左深度,右深度);
}

您非常接近,不需要
depthCurrent
指针

   findthedepth(root->rightp(), depthtotal /*, depthcurrent*/);
   right = *depthtotal; // update the total depth captured in right

   // *depthcurrent = 0; (no use)

   findthedepth(root->leftp(), depthtotal /*, depthcurrent*/);
   left = *depthtotal; // update the total depth captured in left

我建议删除这些指针,只返回值。它使代码更容易阅读,并且可能更容易发现问题。在非空的情况下,您似乎从来没有实际写入到
depthcurrent
,所以请将其删除。然后,您可以通过执行
findthedepth(root->rightp(),&right)
(对于left也是如此)来修复递归调用。最后一次写入
depthtotal
是正确的。明确地切换到直接返回值或引用
depthcurrent