c++ - 二叉树中节点的级别
问题描述
为什么没有打印节点的级别?每次调用后级别都会增加?代码中的问题在哪里?
int levelNode(struct Node* root,int a,int level){
if(root==NULL){
return 0;
}
if(root->key==a){
return level;
}
levelNode(root->left,a,level+1);
levelNode(root->right,a,level+1);
}
解决方案
应该像
int levelNode(struct Node* root,int a,int level){
int found;
if(root==NULL){
return 0;
}
if(root->key==a){
return level;
}
found = levelNode(root->left,a,level+1);
if (found != 0) return found;
found = levelNode(root->right,a,level+1);
if (found != 0) return found;
return 0;
}
推荐阅读
- c++ - WINAPI identifiers in function declaration C++ in DLL entrypoint function
- gstreamer - gst-launch-1.0 和 GStreamer 版本不匹配
- laravel - access auth for boot method to use variable in master blade
- jquery - 无法使用添加标题活动链接在 jquery 中读取属性“拆分”
- python - 如何遍历多个 URL 以从 Scrapy 中的 CSV 文件中抓取?
- android - 运行 studio.sh 时 Android Studio 不显示 ui
- apache - /error 目录不可访问
- azure-functions - 用于加载 blob 并将其直接发送到 context.res 的 Azure 函数
- r - 如何控制 autoplot.forecast 中的置信区间?
- sequelize.js - FindAll where to_days('createdAt') = to_days(now()) with sequelizejs