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的更多相关文章

  1. [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 ...

  2. [geeksforgeeks] Lowest Common Ancestor in a Binary Search Tree.

    http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/ Lowest Common Ancestor ...

  3. [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 ...

  4. [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 ...

  5. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  6. 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 ...

  7. 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 ...

  8. 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 利用二 ...

  9. 【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 ...

随机推荐

  1. JS与原生OC/Swift相互调用总结

    代码地址如下:http://www.demodashi.com/demo/12754.html JS-OC-Swift JS和OC/Swift相互调用,主要总结了JS和OC交互的三种方式 1.使用UI ...

  2. unsigned int与int相加问题

    作者 : 卿笃军 一道unsigned int与int类型的相加题目.引发了我对这个问题的思考. 首先要明确两个问题: 问题一. unsigned int 和 int究竟哪个能表达出来的数上限大呢? ...

  3. swift 可变參数

    func sumof(numbers:Int...)->Int{  var sum = 0;  for number in numbers{   sum+=number;  }  return ...

  4. maven nexus myeclipse 学习

    http://b-l-east.iteye.com/blog/1246482 这篇文章比较详细的介绍了 nexus 本地仓库以及与maven的配合使用 http://blog.csdn.net/arv ...

  5. IIS7应用程序池集成和经典的区别 对IIS7经典模式和集成模式的理解 程序池经典和集成的区别

    IIS7应用程序池集成和经典的区别   IIS7应用程序池集成和经典的区别 IIS7应用程序池有集成和经典两种模式,根据微软官方的介绍, 集成模式,如果托管应用程序在采用集成模式的应用程序池中运行,服 ...

  6. 一个队列类的实现(比delphi自带的速度快70倍)(线程安全版本)

    unit sfContnrs; interface {$DEFINE MULTI_THREAD_QUEUE} //线程安全版本,如果不需要线程安全,请注释掉此行代码 {$IFDEF MULTI_THR ...

  7. sql语句偶记录

    查看表结构: show FULL COLUMNS FROM tablename;

  8. 三星DRAM+NAND FLASH 合成MCP芯片介绍及应用攻略

    转自:http://blog.csdn.net/gao5528/article/details/6256119 三星DRAM+NAND FLASH 合成MCP芯片介绍及应用攻略(K5系列产品篇) 一年 ...

  9. 【JMeter4.0学习(八)】之断言

    目录 响应断言 一.响应断言 1.添加线程组 2.添加HTTP请求默认值 3.添加HTTP请求1 4.先运行“HTTP请求1”,查看结果树的“取样器结果.请求.响应数据” ①取样器结果 ②请求 ③响应 ...

  10. 如何在github上发起一个pull request,如何贡献代码,参与开源项目

    点击页面右上角的 “fork” ,把你关注的项目fork到你自己的账号下了. 把项目克隆到本地 修改并push 回到你的github界面,发起请求: 在自己fork的库处新建请求:New pull r ...