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 ...
随机推荐
- python 存取xml方法
或者也可以参考http://www.cnblogs.com/xiaowuyi/archive/2012/10/17/2727912.html中内容 目前而言,Python 3.2存取XML有以下四种方 ...
- viewDidLayoutSubviews在ios7上导致应用崩溃
在ios8中使用viewDidLayoutSubviews,应用正常运行,没有问题,但是应用在ios7上运行的时候,报错,导致应用崩溃,错误信息类似: Cannot find executable f ...
- VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService 未找到与约束ContractName,无法打开项目的解决方案 SQLyog 注册码
VS2012 未找到与约束ContractName Microsoft.VisualStudio.Text.ITextDocumentFactoryService 最近新换了系统还真是问题多多呀! ...
- ArcObject IFeature set_Shape()和Delete()报错
这样的问题主要是Ifeature实际在数据库里面不存在!可是通过IFeatureClass.getFeature()又可以得到! 详细操作流程: 首先是对要素进行删除,可是通过IFeatureClas ...
- 如何去掉MapReduce输出的默认分隔符
我们在用MapReduce做数据处理的时候,经常会遇到将只需要输出键或者值的情况,如context.write(new Text(record), new Text("")),这样 ...
- xgboost 特征选择,筛选特征的正要性
import pandas as pd import xgboost as xgb import operator from matplotlib import pylab as plt def ce ...
- appium 学习和环境搭建
官方网站: http://appium.io/ 1.安装各大开发环境:Nodejs. python .java 和 android 环境,并且配置环境变量. ✔ The Node.js binary ...
- listView的异步加载数据
1 public class MainActivity extends Activity { 2 3 private ListView listView; 4 private ArrayList< ...
- Google Code Jam 2014 Round 1 A:Problem C. Proper Shuffle
Problem A permutation of size N is a sequence of N numbers, each between 0 and N-1, where each numbe ...
- px与与rem vw的区别
1.px 使用具体像素点为单位,好处是比较稳定和精确,但在浏览器放大缩小会出现问题 2.rem 参考根元素的值 例如设置根元素字体大小是20像素 在h1中设置字体大小 那么H1的大小就是40px p的 ...