博客
关于我
252-查找二叉树某节点的双亲
阅读量:533 次
发布时间:2019-03-08

本文共 612 字,大约阅读时间需要 2 分钟。

<BtNode* Parent(BtNode* ptr,BtNode* child){if(ptr==nullptr||ptr->leftchild==child||ptr->rightchild==child){return ptr;}else{BtNode* p=Parent(ptr->leftchild,child);if(p==nullptr){p=Parent(ptr->rightchild,child);}return p;}}<BtNode* FindParent(BtNode* ptr,BtNode* child){if(ptr==nullptr||child==nullptr||ptr==child){return nullptr;}else{return Parent(ptr,child);}}

这段代码定义了两个用于查找二叉树节点直接双亲的函数。Parent函数接收两个节点指针:当前节点ptr和待查找的子节点child。如果子节点child是ptr的左孩子或右孩子,则直接返回ptr作为双亲。若child不是左或右孩子,则递归调用Parent函数,继续向上查找。在递归结束时,如果未找到对应的双亲,则返回空指针。

FindParent函数则服务于更为简单的用途,它直接调用Parent函数来确定给定子节点child的直接双亲。该函数通过类似逻辑排除了一些无效情况,确保只返回有效的双亲节点。

转载地址:http://wtyiz.baihongyu.com/

你可能感兴趣的文章
nginx安装与配置
查看>>
Nginx安装及配置详解
查看>>
nginx安装并配置实现端口转发
查看>>
nginx安装配置
查看>>
Nginx实战之1.1-1.6 Nginx介绍,安装及配置文件详解
查看>>
Nginx实战经验分享:从小白到专家的成长历程!
查看>>
nginx实现二级域名转发
查看>>
Nginx实现动静分离
查看>>
Nginx实现反向代理负载均衡
查看>>
nginx实现负载均衡
查看>>
Nginx将https重定向为http进行访问的配置(附Demo)
查看>>
nginx工作笔记004---配置https_ssl证书_视频服务器接口等
查看>>
nginx常用命令及简单配置
查看>>
Nginx常用屏蔽规则,让网站更安全
查看>>
nginx开机启动脚本
查看>>
nginx异常:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
查看>>
nginx总结及使用Docker创建nginx教程
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in /usr/local/nginx/conf/nginx.conf:128
查看>>
nginx报错:the “ssl“ parameter requires ngx_http_ssl_module in usrlocalnginxconfnginx.conf128
查看>>
nginx日志分割并定期删除
查看>>