Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree
http://www.geeksforgeeks.org/lowest-common-ancestor-binary-tree-set-1/
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; bool findpath(node *root, vector<int> &path, int n) {
if (!root) return false;
path.push_back(root->data);
if (root->data == n) return true;
if (root->left && findpath(root->left, path, n) || root->right && findpath(root->right, path, n)) return true;
path.pop_back();
return false;
} int LCA(node *root, int n1, int n2) {
if (!root) return -;
vector<int> path1, path2;
if (!findpath(root, path1, n1) || !findpath(root, path2, n2)) return -;
int i = ;
for (; i < path1.size() && i < path2.size(); i++) {
if (path1[i] != path2[i]) break;
}
return path1[i-];
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
cout << "LCA(4, 5) is " << LCA(root, , ) << endl;
cout << "LCA(4, 6) is " << LCA(root, , ) << endl;
cout << "LCA(3, 4) is " << LCA(root, , ) << endl;
cout << "LCA(2, 4) is " << LCA(root, , ) << endl;
return ;
}
one traversal
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; node* LCA(node *root, int n1, int n2) {
if (!root) return NULL;
if (root->data == n1 || root->data == n2) return root;
node *l = LCA(root->left, n1, n2);
node *r = LCA(root->right, n1, n2);
if (l && r) return root;
return l? l : r;
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
cout << "LCA(4, 5) is " << LCA(root, , )->data << endl;
cout << "LCA(4, 6) is " << LCA(root, , )->data << endl;
cout << "LCA(3, 4) is " << LCA(root, , )->data << endl;
cout << "LCA(2, 4) is " << LCA(root, , )->data << endl;
return ;
}
上面这段代码有些问题,当n1或者n2不存在时应该回NULL,但是回的是n1或者n2
#include <iostream>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <string>
#include <fstream>
#include <map>
using namespace std; struct node {
int data;
struct node *left, *right;
node() : data(), left(NULL), right(NULL) { }
node(int d) : data(d), left(NULL), right(NULL) { }
}; node* _LCA(node *root, int n1, int n2, bool &v1, bool &v2) {
if (!root) return NULL;
if (root->data == n1) {
v1 = true;
return root;
}
if (root->data == n2) {
v2 = true;
return root;
}
node *l = _LCA(root->left, n1, n2, v1, v2);
node *r = _LCA(root->right, n1, n2, v1, v2);
if (l && r) return root;
return l? l : r;
} bool findnode(node *root, int k) {
if (!root) return false;
return (root->data == k || findnode(root->left, k) || findnode(root->right, k));
} node *LCA(node *root, int n1, int n2) {
bool v1 = false;
bool v2 = false;
node *lca = _LCA(root, n1, n2, v1, v2);
if (v1 && v2 || v1 && findnode(root, n2) || v2 && findnode(root, n1)) return lca;
return NULL;
} int main() {
node *root = new node();
root->left = new node();
root->right = new node();
root->left->left = new node();
root->left->right = new node();
root->right->left = new node();
root->right->right = new node();
cout << "LCA(4, 5) is " << LCA(root, , )->data << endl;
cout << "LCA(4, 6) is " << LCA(root, , )->data << endl;
cout << "LCA(3, 4) is " << LCA(root, , )->data << endl;
cout << "LCA(2, 4) is " << LCA(root, , )->data << endl;
return ;
}
Data Structure Binary Tree: Lowest Common Ancestor in a Binary Tree的更多相关文章
- [CareerCup] 4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点
4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tr ...
- [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.
http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...
- [LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点
Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According ...
- [LeetCode] Lowest Common Ancestor of a Binary Search Tree 二叉搜索树的最小共同父节点
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree
leetcode面试准备:Lowest Common Ancestor of a Binary Search Tree & Binary Tree 1 题目 Binary Search Tre ...
- Lowest Common Ancestor of a Binary Search Tree、Lowest Common Ancestor of a Binary Search Tree
1.Lowest Common Ancestor of a Binary Search Tree Total Accepted: 42225 Total Submissions: 111243 Dif ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
随机推荐
- CoreImage的模糊滤镜
//1.原始图片 UIImage * image = [UIImage imageNamed:@"1.jpg"]; /****************core image***** ...
- UITextView 设置边框
UITextView * txtView = [[UITextView alloc] initWithFrame:CGRectMake(10, 50, 200, 50)]; txtVi ...
- html实现网站全局按钮点击后置灰,不允许连续点击
<script> document.addEventListener("mouseup", upHandler, true); function upHandler(e ...
- Python修改文件权限
os.chmod()方法 此方法通过数值模式更新路径或文件权限.该模式可采取下列值或按位或运算组合之一: stat.S_ISUID: Set user ID on execution. stat.S_ ...
- CoreAnimation的使用小结
參考:http://www.cnblogs.com/wendingding/p/3801157.htmlhttp://www.cnblogs.com/wendingding/p/3802830.htm ...
- nginx 的uri、request_uri 区别
在nginx中有几个关于uri的变量,包括$uri $request_uri $document_uri,下面看一下他们的区别 : $request_uri: /stat.php?id=1585378 ...
- CSS改变字体下划线颜色
下图是网页中一个非常普通的列表. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQXVndXMzMzQ0/font/5a6L5L2T/fontsize/40 ...
- 【v2.x OGE教程 11】 动画编辑器帮助文档
] 动画编辑器帮助文档 版本号 日期 作者 说明 1.0 2014-9-3 橙子游戏 文档创建 一.简单介绍 动画编辑器用于游戏动画的可视化编辑,支持序列帧动画和关键帧动画.通过解析生成的 ...
- local variable 'xxx' referenced before assignment(犯过同样的错)
这个问题很囧,在外面定义了一个变量 xxx ,然后在Python的一个函数里面引用这个变量,并改变它的值,结果报错local variable 'xxx' referenced before assi ...
- 一篇文章彻底弄清ARC始末
本文转载至 http://blog.csdn.net/allison162004/article/details/38758265 自动引用计数(ARC)是编译器的一个特色,提供了Objective- ...