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. Effective JAVA 创建和销毁对象 遇到多参构造器考虑使用构建器

    //构建器抽象类,为不同类的实现提供 public interface Builder<T> { public T build(); } /** * @描述: 营养表 */ public ...

  2. 文件——文件指针——文件练习(随机产生N个手机号)——自动关文件

    python 2 3file() #python 2读模式 写模式 追加模式 只要沾上了r,文件不存的就会报错读模式 r 读写模式 r+(可以写)1.不能写2.文件不存在报错f=open('123', ...

  3. asp.net core mvc视频A:笔记3-4.母版页与部分视图

    新建项目3.4, 新建一个共享文件,一般存放在Shared目录下方 选择 如果安装了Reshaper插件可以这样添加(插件在本人博客中找) 代码 创建一个空的控制器TestController 使用布 ...

  4. 链表的艺术——Linux内核链表分析

    引言: 链表是数据结构中的重要成员之中的一个.因为其结构简单且动态插入.删除节点用时少的长处,链表在开发中的应用场景许多.仅次于数组(越简单应用越广). 可是.正如其长处一样,链表的缺点也是显而易见的 ...

  5. Mqtt协议IOS端移植2

    MqttFramework.h #import <Foundation/Foundation.h> #import "MQTTClient.h" #import &qu ...

  6. Linux下基于命令行的抓包方法

    大家可能都已经对著名的抓包工具Ethereal比较熟悉了,这里再介绍一种基于命令行的抓包工具tcpdump. 举例:抓本机1813端口上的数据,并将抓包结果保存在test.cap文件中 然后在本地可以 ...

  7. VMware Mac OS补丁安装

    安装了VMware9.0在新建虚拟系统的时候,没有Appel MAC OS系统的选项,上网查了一下是需要打一个VMware Mac OS补丁就可以了.下面我来演示一下VMware Mac OS补丁怎么 ...

  8. AR实景购物强势来袭,华为nova3让你试完再买!

    没空买家具?没空挑壁纸? 浴盆挑了仨星期,还是老样子! 不敢下手买,没空往回退, 这样的生活,你说累不累! 别愁了, 按华为nova3说的办, 可省千元退货费! 刚刚过去的华为nova3发布会上,华为 ...

  9. laravel学习之路2: jwt集成

    "tymon/jwt-auth": "^1.0@dev", 执行 composer update 'providers' => [ .... Tymon\ ...

  10. WPF编程学习——样式(好文)

    http://www.cnblogs.com/libaoheng/archive/2011/11/20/2255963.html