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

    mongodb存储的是文档,文档是json格式的对象,我们的增删改查,都要传输json对象 json是一个对象,js里有数组这个概念,只需要把多个对象放到一个数组里,即可 use test //首先选 ...

  2. 《windows核心编程》 在应用程序中使用虚拟内存

    Microsoft Windows 提供了以下三种机制来对内存进行操控: 虚拟内存 最适合用来管理大型对象数组或大型结构数组 内存映射文件 最适合用来管理大型数据流(通常是文件),以及在同一台机器上运 ...

  3. 事务(Transaction)概念和特性

    http://baike.baidu.com/view/121511.htm 概念 事务(Transaction)是访问并可能更新数据库中各种数据项的一个程序执行单元(unit).事务通常由高级数据库 ...

  4. HDU-1165-Eddy&#39;s research II

    这个事实上是一个递归题.题目非常easy.m的数非常小.分三种情况.算一下.就能够直接把公式算出来. 当然,也能够用dp做: #include<iostream> #include< ...

  5. Hadoop2.6.0子项目hadoop-mapreduce-examples的简介

    引文 学习Hadoop的同学们,一定知道假设执行Hadoop自带的各种样例,以大名鼎鼎的wordcount为例,你会输入下面命令: hadoop org.apache.hadoop.examples. ...

  6. iOS中文输入法多次触发的问题及解决方案

    最近要在移动端实现一个文本框实时搜索的功能,即在文本框里每输入一个字,就向服务器请求一次搜索结果.暂且不考虑性能优化问题,第一时间想到的是用keyup实现: $('input').on('keyup' ...

  7. 腾讯课堂十大Excel函数

    十大函数:if,sumifs,countifs,vlookup,match,index,indirect,subtotal,left(mid,right),offset substotal:用于灵活计 ...

  8. centos7.2 开发 部署 .net core

    1.centos7.2 安装 nginx官方文档:http://nginx.org/en/linux_packages.html#mainline 创建nginx.repo 文件 Pre-Built ...

  9. linux 中vim学习与总结

    平常使用vim总是忘记快捷键,在这里做一个总结一下比较常用的快捷把,省的每次都要去查. h : 向左移动一个字符(←) j : 向上移动一个字符(↑) k : (↓) l : (→) ctrl+f : ...

  10. Mac OS X 安装Ruby

    安装CocoaPods第一步 起因:重装系统后需要重新安装CocoaPods网上搜了下发现很多都过时了,已经不能用了.而且taobao Gems源已经停止服务,现在有ruby-china提供服务 PS ...