Find Common Ancestor

Vue3 + OpenLayers 开发指南(一) - 环境搭建与基础配置 本文详细介绍了如何从零开始搭建Vue3与OpenLayers的集成开发环境。通过项目初始化、核心依赖安装、目录结构规划及基础配置,手把手引导开发者创建一个结构清晰、可维护的现代化WebGIS应用骨架,为后续添加地图图层和交互功能奠定坚实基础。 阅读详情

Problem: find the common ancestor of two nodes in a tree

Given a "tree" pointer pointing to the root node of a tree and the other two pointers to two nodes in the tree. You are expected to write a C++ subroutine to find the common ancestor of the two nodes. In any unexpected condition, your code should return null pointer and avoid crashing.

For example, if the tree is like below, B is the common ancestor of E and G; A is the common ancestor of H and F; D is the common ancestor of D and G.

You are allowed to use recursion but as few as possible. You are NOT allowed to use STL. Please follow the type definitions shown below:

//tree node type
typedef struct _Node { char value; struct _Node* left; struct _Node* right; }Node; class CommonNodeFinder { //add any auxiliary code here as you wish. public: inline Node* FindCommonAncestor(Node* tree, Node* node_1, Node* node_2) { //your code here. } }
my solution:

#include <iostream>

template< typename T >

class stack{ 

public:

       struct Node{

              T value;

              Node* next;

       };

public:

       stack()

       {

              phead = NULL;

       }

       virtual ~stack()

       {

              Node* pnode = phead;

              while ( pnode != NULL )

              {

                     phead = phead->next;

                     delete pnode;

                     pnode = phead;

              }           

       }

       void push_back( T value )

       {

              Node* ptempnode = new Node;

              ptempnode->value = value;

              ptempnode->next = phead;

              phead = ptempnode;

       }

       T get( ) const

       {

              return phead->value;

       }

       void pop_up( )

       {

              Node* pnode;

              pnode = phead;  

              phead = phead->next;

              delete pnode;

       }

       bool is_empty( ) const

       {

              if ( phead == NULL )

                     return true;

              else

                     return false;

       }

 

private:

       Node* phead;

};

 

template < typename T >

struct Node

{

       T value;

       Node* left;

       Node* right;

};

 

void visit( Node<char>* p )

{

       std::cout << p->value << std::endl;

}

class CommonNodeFinder

{

public:

       // auxiliary function used for generating tree

       void generate_tree( char tree_serial[] )

       {

              int n = strlen( tree_serial );

              if ( n < 1 )

              {

                     tree_phead = NULL;

                     return;

              }

              Node<char>* node;

              node = new Node<char>;

              node->value = tree_serial[0];

              node->left = generate_child( tree_serial, 1, n );

              node->right = generate_child( tree_serial, 2, n );

              tree_phead = node;

              return;

       }

       // auxiliary function used for generating tree

       Node<char>* generate_child( char tree_serial[], const int& index, const int& length )

       {

              if ( index >= length || tree_serial[index] == ' ' )

                     return NULL;

              Node<char>* node;

              node = new Node<char>;

              node->value = tree_serial[index];

              node->left = generate_child( tree_serial, 2 * index + 1 , length );

              node->right = generate_child( tree_serial, 2 * index + 2, length );

              return node;

       }

       // auxiliary function used for visit the tree

       void pre_order_traverse( Node<char>* pnode, void ( *visit ) ( Node<char>* p ))

       {

              if( pnode != NULL )

              {

                     visit( pnode );

                     pre_order_traverse( pnode->left, visit );

                     pre_order_traverse( pnode->right, visit );

              }

       }

 

       // auxiliary function used for delete the tree

       void delete_tree( Node<char>* pnode )

       {

              if ( pnode != NULL )

              {

                     delete_tree( pnode->left );

                     delete_tree( pnode->right );

                     delete pnode;

              }

       }

       // get the Node by the value

       Node<char>* find_in( char val, Node<char>* root )

       {

              Node<char>* node;

              if( root == NULL )

                     return NULL;

              else if ( root->value == val )

                     return root;

              else if ( ( node = find_in( val, root->left ) ) != NULL )

                     return node;

              else if ( ( node = find_in( val, root->right ) ) != NULL )

                     return node;

              else

                     return NULL;

       }

       //generator the ancestor stack for node in the root

       bool is_belong_to( Node<char>* node, Node<char>* root, stack< Node<char>* >& ancestors )

       {

              if ( root == NULL )

                     return false;

              else if ( node == root )

              {

                     ancestors.push_back( root );

                     return true;

              }

              else if ( is_belong_to( node, root->left, ancestors ) )

              {

                     ancestors.push_back( root );

                     return true;

              }

              else if ( is_belong_to( node, root->right, ancestors ) )

              {

                     ancestors.push_back( root );

                     return true;

              }

              else

                     return false;

       }

 

public:

       Node<char>* tree_phead;

       CommonNodeFinder( )

       {

              tree_phead = NULL;

       }

       virtual ~CommonNodeFinder( )

       {

              delete_tree( tree_phead );

       }

public:

       inline Node<char>* FindCommonAncestor(Node<char>* tree, Node<char>* node_1, Node<char>* node_2)

       {

              stack< Node<char>* > ancestor1;

              stack< Node<char>* > ancestor2;

              is_belong_to( node_1, tree, ancestor1 );

              is_belong_to( node_2, tree, ancestor2 );

              if ( ancestor1.is_empty() )

                     return NULL;

              if ( ancestor2.is_empty() )

                     return NULL;

              Node<char> *a1, *a2, *a;

              a1 = ancestor1.get();

              a2 = ancestor2.get();

              while ( a1 == a2 )

              {

                     a = a1;

                     ancestor1.pop_up();

                     if ( ancestor1.is_empty() )

                            break;

                     ancestor2.pop_up();

                     if ( ancestor2.is_empty() )

                            break;

                     a1 = ancestor1.get();

                     a2 = ancestor2.get();

              }

              return a;

       }

};

// test code

int main ( int argc, char* argv[] )

{

       CommonNodeFinder NodeFinder;

       Node<char>* node1;

       Node<char>* node2;

       Node<char>* node;

 

       NodeFinder.generate_tree( "ABCDE HFG" );

       node1 = NodeFinder.find_in( 'D', NodeFinder.tree_phead );

       node2 = NodeFinder.find_in( 'G', NodeFinder.tree_phead );

       node = NodeFinder.FindCommonAncestor( node1, NodeFinder.tree_phead, node2 );

       if ( node )

              std::cout << node->value << std::endl;

}

 

NXP(恩智浦)—TJA1043TK CAN收发器芯片详解 本文章主要讲解NXP(恩智浦)—TJA1043TK CAN收发器的相关知识。 阅读详情

相关推荐

Computer Networks 和 Computer & Security投稿经验

论文投稿

qq_50604294的博客 9238

Find the First Common Ancestor

class Tnode{ Tnode parent; Tnode left; Tnode right; int val; } //if it has parent node public Tnode ancestor(Tnode node1, Tnode node2) { if(node1 == null || node2 == null) { retu

Gohusky! 696

Python海康摄像头SDK二次开发

摘要:为解决海康威视摄像头RTSP拉流延迟严重的问题,本文提出改用海康私有SDK直接取流的方案。通过下载海康设备网络SDK并调用其Python接口,实现了低延迟的视频流获取。文中详细介绍了SDK配置流程,并提供了优化版的Python代码示例,重点解决回调函数处理、队列缓冲和CPU占用等问题。测试结果表明,该方案显著降低了延迟,同时保持了较高的帧率(30FPS以上)和较低的CPU占用率(约2%)。相较于传统OpenCV+FFmpeg的RTSP方案,海康SDK直接取流在实时性方面具有明显优势。

xiaohouzi112233的博客 2609

1143. Lowest Common Ancestor

Lowest Common Ancestor The lowest common ancestor (LCA) of two nodes U and V in a tree is the deepest node that has both U and V as descendants. A binary search tree (BST) is recursively defined as ...

Mapoos 552

服务器报错:“Unhealthy status reported by this UEFI driver without specific error message”.. 记录解决过程

服务器开机报错:Unhealthy status reported by this UEFI driver without specific...

ayychiguoguo的博客 4401

【知识分享】CAN总线中终端网络的重要性

P沟道和N沟道晶体管的漏源通态电阻匹配良好时,形成的CANH和CANL信号匹配良好;如果驱动器的一半具有比另一半低得多的导通状态电阻,那么总线共模电压将从VCC/2的显性状态偏离,并回到VCC/2的隐性状态,这将导致每一次状态转换都会伴随着共模电压噪声,从而导致更高水平的传导和辐射能量发射。

一只豌豆象的博客 1548

算法题:Find the closest common ancestor

二叉树公共祖先

机器学习,深度学习 3880

学习STM32的DS18B20温度传感器

该函数首先复位传感器,然后发送跳过ROM命令和转换命令,并等待转换完成。接下来,它再次复位传感器,发送跳过ROM命令和读取温度命令。最后,它读取温度值的最低字节和最高字节,并将其组合成16位温度值。传感器的VCC引脚连接到STM32的3.3V电源,GND引脚连接到GND,而数据引脚连接到任何一个IO引脚。需要注意的是,示例代码中使用了一个简单的延时函数来等待转换完成。实际上,您可能需要使用定时器或其他更精确的延时方法来实现更准确的延时。下面是一个示例代码,演示如何读取DS18B20传感器的温度值。

m0_75244442的博客 1184

Lowest Common Ancestor问题的解决思路

对于Off-Line的Lowest Common Ancestor问题,问题描述如下:   we are given a rooted tree T and an arbitrary set P{(u,v),...} of unordered pairs of nodes in T , and we wish to deter- mine the least common ancestor o

Pandoras Box 3800

华为数字化转型之道 方法篇 第五章 视IT为投资,用产品思维管理IT

比较传统信息化和数字化转型下的IT系统特征,我们发现业务环境、IT能力、业务和IT的关系都发生了巨大的变化(见图5-1)从“管理系统”到“作业平台”。企业通常的做法是将线下的文档、流程线上化,IT系统是作为管理系统而存在的。而面向数字化,企业的业务作业、业务运营和决策、团队协同等业务动作已经和IT系统集成,IT系统逐步转变为作业系统。从“功能优先”到“体验优先”,如果要整体完成一项工作往往需要用户在不同系统间切换,费时费力。

沧海之巅的专栏 1095

RT-DETR改进策略【注意力机制篇】| 2024 PPA 并行补丁感知注意模块,提高小目标关注度

本文记录的是利用改进检测精度,详细说明了优化原因,注意事项等。原论文在红外小目标检测任务中,小目标在多次下采样操作中容易丢失关键信息。通过替代编码器和解码器基本组件中的传统卷积操作,更好地保留小目标的重要信息。HCF-Net: Hierarchical Context Fusion Network for Infrared Small Object Detection采用多分支特征提取策略,通过不同分支提取不同尺度和层次的特征。利用局部、全局和串行卷积分支,对输入特征张量进行处理。通过控制 patch si

Limiiiing的博客 1648

最近公共祖先LCA问题

问题描述 求有根树的任意两个节点的最近公共祖先。 分析与解法 解答这个问题之前,咱们得先搞清楚到底什么是最近公共祖先。最近公共祖先简称LCA(Lowest Common Ancestor),所谓LCA,是当给定一个有根树T时,对于任意两个结点u、v,找到一个离根最远的结点x,使得x同时是u和v的祖先,x 便是u、v的最近公共祖先。(参见:http://en.wikipedia.o

Al_xin的专栏 2571

Claude Code Cli 源码泄露事件全解析:从51万行代码中拆解AI Agent的记忆护城河

2026年3月,Anthropic的AI编程助手Claude Code意外泄露51万行核心代码,暴露了其多Agent架构、六级权限验证系统及创新的记忆机制。记忆系统采用四层设计:用户自定义框架、自动记忆提取、智能上下文压缩和纯文件存储。最独特的是"自动做梦"功能,AI会定期整理记忆,类似人类睡眠记忆巩固。代码还揭示未发布功能如电子宠物系统,但也暴露权限系统漏洞和代码质量问题。这次泄露展现了AI助手核心技术的复杂性,特别是其记忆系统如何通过透明可编辑的设计建立用户信任。

program哲学 472

ValueError: Could not find common ancestor of[]

如果在 PowerShell 中使用 cp -r core/migrations/ core/migrations_backup/ 命令时,目标目录 core/migrations_backup/migrations 已经存在,因此 cp 无法覆盖它。✅ 步骤 4:重新生成迁移(Django 会以当前模型为基础生成一个新初始迁移)✅ 步骤 3:清除数据库中记录的迁移历史(保留数据)✅ 步骤 5:伪造执行迁移(保留数据库结构和数据)✅ 步骤 1:备份当前迁移文件(可选但推荐)FIND: 参数格式不正确。

qq_49894233的博客 341

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

Camera2Demo

在Camera2 的基础上进行封装,使得Camera2一堆初始化逻辑与Activity分离,降低耦合度,完成预览拍照并保存的操作

fwnx- find the common ancestor in a tree

1 if the nodes are in the left tree then find the common ancestor in the left tree 2 if the nodes are in the right tree then find the common ancestor in the right tree 3 if the nodes are in differen

kingoverlord的专栏 938

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

leetcode -- find the lowest common ancestor

Given a binary tree, find the lowest common ancestor of two given nodes in the tree. _______3______ / \ ___5__ ___1__ / \ / \ ...

weixin_34352005的博客 76

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

Find the common ancestor of two nodes in a binary tree

http://stackoverflow.com/questions/1484473/how-can-i-find-the-common-ancestor-of-two-nodes-in-a-binary-tree TBA

AlgorithmEngine 704

Find the nearest common ancestor of any two nodes in a binary search tree

自己写的,贴出来show一下,接受拍砖.1. creat binary tree  static void CreateBinaryTree(int i, int? isOut = null){  if (binaryRootNode == null){  BinaryNode node = new BinaryNode();

luozhongrong_06的专栏 576

Binary Search Tree--find Lowest Common Ancestor--C++ and Java

Question is from here. Given a binary search tree (BST), find the lowest common ancestor of two given nodes in the BST.  _______6______ / \ ___2__ ___8__

huizi31662003的专栏 655

git branch/tag search with commited id///Find the most recent common ancestor of two branches

1.git branch --contains <commit> $ git branch --contains d590f2 tests * master git branch --contains <commit> Only list branches which contain the specified commit (HEAD if not...

hushui的专栏 317

LeetCode | 530.Minimum Absolute Diff in BST, 501.Find Mode in BST,236.Lowest Common Ancestor of a BT

Link: https://leetcode.com/problems/minimum-absolute-difference-in-bst/Given the of a Binary Search Tree (BST), return the minimum absolute difference between the values of any two different nodes in the tree.The result of inorder traversal of a BST is an

weixin_43615320的博客 100

代码随想录算法训练营第21天 |Binary Tree|● 530.二叉搜索树的最小绝对差 ● 501.二叉搜索树中的众数 ● 236. 二叉树的最近公共祖先

【代码】代码随想录算法训练营第21天 |Binary Tree|● 530.二叉搜索树的最小绝对差 ● 501.二叉搜索树中的众数 ● 236. 二叉树的最近公共祖先。

weixin_39248851的博客 231

Lowest Common Ancestor III

Given the root and two nodes in a Binary Tree. Find the lowest common ancestor(LCA) of the two nodes. The lowest common ancestor is the node with largest depth which is the ancestor of both nodes. Ret...

leetcode 解题思路 301

Lowest Common Ancestor (LCA) of two nodes in graph

In graph theory and computer science, the lowest common ancestor (LCA) of two nodes v and w in a tree or directed acyclic graph (DAG) is the lowest (i.e. deepest) node that has both v and w

Race6006的专栏 863

LeetCode_Lowest Common Ancestor

题目:给出一颗二叉树中的节点n1和n2,要求

loveRooney的专栏 2935

[CTCI] 4.6 First Common Ancestor for Two Nodes On a Binary tree

问题: Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary sear

seemuch的专栏 1505
上一篇: 重温C++之虚拟析构函数
下一篇: Partially Revert Linked List
peterfly
博客等级 码龄25年 0粉丝 7原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值