每日一题系列 - 二叉树节点求共同祖先
2009-10-12 23:00
337 查看
每天,自我肯定?还是自我否定?”
『每日一题』
题目取自清华大学《数据结构》题集,严蔚敏,米宁, 吴伟民
已知在二叉链表中,root为根节点, p^和q^为二叉树中两个节点,试编写求距离他们最近的共同祖先的算法。
============
题解:
如果此题的树结构是双向的,即子节点保留父节点的指针,父节点有指向子节点, 那么可以很快求解。现在考虑单向的情形。
用递归不可取,会大大增加算法复杂度。
将二叉链表实现为单向二叉树:
struct Tree
{
int value;
struct Tree * left;
struct Tree * right;
}
由于树的生长是单向向下的,子节点无法知道父亲节点,所以要分别通过遍历p^, q^得到其具体路径。
写了一个函数,能够实现二叉树的遍历,同时能够将到达目标节点所进过的路径压入栈,并将栈作为返回值
通过这个函数,得到栈以后(根节点在栈底,目标节点在栈头),
stack_p=bianli(root,p);
stack_q=bianli(root,q);
再通过一次出栈、入栈操作,将其反序,得到:
stack_p.reverse()
stack_q.reverse()
然后再用:
『每日一题』
题目取自清华大学《数据结构》题集,严蔚敏,米宁, 吴伟民
已知在二叉链表中,root为根节点, p^和q^为二叉树中两个节点,试编写求距离他们最近的共同祖先的算法。
============
题解:
如果此题的树结构是双向的,即子节点保留父节点的指针,父节点有指向子节点, 那么可以很快求解。现在考虑单向的情形。
用递归不可取,会大大增加算法复杂度。
将二叉链表实现为单向二叉树:
struct Tree
{
int value;
struct Tree * left;
struct Tree * right;
}
由于树的生长是单向向下的,子节点无法知道父亲节点,所以要分别通过遍历p^, q^得到其具体路径。
写了一个函数,能够实现二叉树的遍历,同时能够将到达目标节点所进过的路径压入栈,并将栈作为返回值
#include "stdafx.h" #include <iostream> using namespace std; #include <stack> struct Tree { int value; struct Tree * left; struct Tree * right; } stack<Tree *> bianli(Tree * root, Tree * target) { stack<Tree *> s; stack<Tree *> path; Tree * node=root; //先根遍历 while(true) { cout<<"current node: "<<node->value<<endl; path.push(node); if(target==node){ cout<<"find the node"<<endl; return path; } if(NULL!=node->left) { if(NULL!=node->right) { s.push(node->right); } node=node->left; } else if(NULL!=node->right) { node=node->right; } else if(!s.empty()) { while(path.top()->right!=s.top()) path.pop(); node=s.top(); s.pop(); } else { break; } } return path; }
通过这个函数,得到栈以后(根节点在栈底,目标节点在栈头),
stack_p=bianli(root,p);
stack_q=bianli(root,q);
再通过一次出栈、入栈操作,将其反序,得到:
stack_p.reverse()
stack_q.reverse()
然后再用:
Tree * coFather; while(stack_p.top()==stack_q.top()) { coFather=stack_p.top(); stack_p.pop(); stack_q.pop(); } //出循环以后, coFather就是答案
相关文章推荐
- 二叉树系列——两个节点的最低公共祖先
- 二叉树系列 - 求两节点的最低公共祖先,例 剑指Offer 50
- 查找二叉树某两个节点最近的共同祖先及改进方法
- 算法:在二叉树中寻找两个节点的共同祖先
- 算法:在二叉树中寻找两个节点的共同祖先
- 利用栈求二叉树中两节点的最近共同祖先(无父节点指针)
- 二叉树系列---在二叉树中找到两个节点的最近公共祖先
- LCA二叉树任意两节点的共同祖先
- 二叉树系列——路径系列:打印二叉树两个子节点之间的路径以及最低公共祖先
- 二叉树中寻找共同节点的最低公共祖先节点
- Cracking-- 4.7 在一颗二叉树中找两个节点的第一个共同祖先
- 利用栈结构实现二叉树的非递归遍历,求二叉树深度、叶子节点数、两个结点的最近公共祖先及二叉树结点的最大距离
- 二叉树中两个节点的最近公共祖先节点
- 剑指offer系列之56:二叉树的下一个节点
- 二叉树中根结点到某节点的路径(求某结点的所有祖先)
- 20170928_二叉树中两个节点的最低公共祖先
- 235. Lowest Common Ancestor of a Binary Search Tree | 二叉查找树两个节点的最低共同祖先
- 打印二叉树中一个节点的所有祖先节点
- 二叉树系列——路径系列:根节点到子节点的路径以及根节点到叶子节点的所有路径
- 树中两个节点最低公共祖先系列算法