编写函数,以一棵树的跟为输入,返回一棵对应的二叉树C++

编写函数,以一棵树的跟为输入,返回一棵对应的二叉树
2025-05-10 20:47:37
推荐回答(1个)
回答1:

//输入根节点root和子节点node //返回node所在的层数,不存在返回0 unsigned int node_depth(Tree* root, Tree* node){ int d; if(root==NULL) return 0; if(root==node) //在根节点处,即第1层 return 1; d = node_depth(root->left_child, node);//找左节点 if(d>0) return d+1; //返回当前层数+1 d = node_depth(root->right_child, node); //找右节点 if(d>0) return d+1; //返回当前层数+1 return 0; }