博客
关于我
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 禁止以ip形式访问服务器
查看>>
NGINX 端口负载均衡
查看>>
Nginx 结合 consul 实现动态负载均衡
查看>>
Nginx 负载均衡与权重配置解析
查看>>
Nginx 负载均衡详解
查看>>
Nginx 负载均衡配置详解
查看>>
nginx 配置 单页面应用的解决方案
查看>>
nginx 配置https(一)—— 自签名证书
查看>>
nginx 配置~~~本身就是一个静态资源的服务器
查看>>
Nginx 配置服务器文件上传与下载
查看>>
Nginx 配置清单(一篇够用)
查看>>
Nginx 配置解析:从基础到高级应用指南
查看>>
Nginx 集成Zipkin服务链路追踪
查看>>
nginx 集群配置方式 静态文件处理
查看>>
nginx+mysql+redis+mongdb+rabbitmq 自动化部署脚本
查看>>
nginx+php的搭建
查看>>
nginx+tomcat+memcached
查看>>
Nginx+Tomcat实现动静分离
查看>>
nginx+Tomcat性能监控
查看>>
nginx+uwsgi+django
查看>>