给出一颗二叉树。找到两个值的最小公共节点。

假设两个值都会在树中出现。

假设可能不会出现的话,也非常easy。就查找一遍看两个值是否在树中就能够了。假设不在就直接返回NULL。

基本思想:就是在二叉树中比較节点值和两个值的大小,假设都在一边(左边或者右边)那么就往下继续查找,否则就是都在同一边了,那么就能够返回这个节点了,这个节点就是最低公共单亲节点了。

參考:http://www.geeksforgeeks.org/lowest-common-ancestor-in-a-binary-search-tree/

#include <stdio.h>
#include <stdlib.h> class LCABST
{
struct Node
{
int data;
Node *left, *right;
Node(int d) : data(d), left(NULL), right(NULL) {}
};
Node *lca(Node *root, int n1, int n2)
{
if (!root) return NULL;
if (n1 < root->data && n2 < root->data)
return lca(root->left, n1, n2);
if (root->data < n1 && root->data < n2)
return lca(root->right, n1, n2);
return root;
} Node *lcaIter(Node *root, int n1, int n2)
{
while (root)
{
if (n1 < root->data && n2 < root->data)
root = root->left;
else if (root->data < n1 && root->data < n2)
root = root->right;
else break;
}
return root;
} Node *root;
public:
LCABST()
{
run();
} void run()
{
// Let us construct the BST shown in the above figure
root = new Node(20);
root->left = new Node(8);
root->right = new Node(22);
root->left->left = new Node(4);
root->left->right = new Node(12);
root->left->right->left = new Node(10);
root->left->right->right = new Node(14); int n1 = 10, n2 = 14;
Node *t = lca(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->data); n1 = 14, n2 = 8;
t = lca(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->data); n1 = 10, n2 = 22;
t = lca(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->data); n1 = 10, n2 = 14;
printf("\nIterative Run:\n");
t = lcaIter(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->data); n1 = 14, n2 = 8;
t = lcaIter(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->data); n1 = 10, n2 = 22;
t = lcaIter(root, n1, n2);
printf("LCA of %d and %d is %d \n", n1, n2, t->data);
}
~LCABST()
{
deleteTree(root);
}
void deleteTree(Node *r)
{
if (!r) return;
deleteTree(r->left);
deleteTree(r->right);
delete r; r = NULL;
}
};

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

Geeks LCA最低公共单亲节点的更多相关文章

  1. LCA最小公共父节点的解题思路

    LCA最小公共父节点解法: 1.二叉搜索树: 中序遍历是升序,前序遍历即按序插入建树的序列. 二叉搜索树建树最好用前序+中序,如果用前序建树,最坏情况会退化为线性表,超时. 最近公共祖先甲级: A11 ...

  2. 剑指Offer(第二版)面试案例:树中两个节点的最低公共祖先节点

    (尊重劳动成果,转载请注明出处:http://blog.csdn.net/qq_25827845/article/details/74612786冷血之心的博客) 剑指Offer(第二版)面试案例:树 ...

  3. 235. Lowest Common Ancestor of a Binary Search Tree(LCA最低公共祖先)

      Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...

  4. [leetcode]236. 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 ...

  5. LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点

    /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...

  6. 剑指offer-第七章面试案例2(树中两个节点的公共祖先节点)

    import java.util.LinkedList; import java.util.Queue; import java.util.Stack; //树中两个节点的最低公共祖先 //第一种情况 ...

  7. 寻找二叉树中的最低公共祖先结点----LCA(Lowest Common Ancestor )问题(递归)

    转自 剑指Offer之 - 树中两个结点的最低公共祖先 题目: 求树中两个节点的最低公共祖先. 思路一: ——如果是二叉树,而且是二叉搜索树,那么是可以找到公共节点的. 二叉搜索树都是排序过的,位于左 ...

  8. 二叉树系列 - 求两节点的最低公共祖先,例 剑指Offer 50

    前言 本篇是对二叉树系列中求最低公共祖先类题目的讨论. 题目 对于给定二叉树,输入两个树节点,求它们的最低公共祖先. 思考:这其实并不单单是一道题目,解题的过程中,要先弄清楚这棵二叉树有没有一些特殊的 ...

  9. 二叉树节点个数,叶子个数,第K层个数,最低公共节点

    1. 节点个数 function getNodeNum(root){ if(root == null){ return 0; } //+1为root的计数 return getNodeNum(root ...

随机推荐

  1. window maven安装(六)

    Maven 实战系列之在Windows上安装Maven Maven是一个优秀的构建工具(类似于 Ant, 但比 Ant 更加方便使用),能帮助我们自动化构建过程,从清理.编译.测试到生成报告,再到打包 ...

  2. TroubleShoot: Excel Services Fix - "The workbook cannot be opened".

    1. 问题描述: 在SharePoint 2013 文档库中打开Excel 文件提示"The workbook cannot be opened" 错误提示框,文档不能正常显示. ...

  3. [转]iOS7 后台执行

    [转自:http://esoftmobile.com/2013/06/23/ios7%E7%A8%8B%E5%BA%8F%E5%90%8E%E5%8F%B0%E8%BF%90%E8%A1%8C/] i ...

  4. ORCLE数据库用户、权限、角色管理

    PS:中括号表示可选项. ORACLE 用户管理 1.创建用户 CREATE USER username   --用户名 IDENTIFIED BY password --密码 [ACCOUNT LO ...

  5. fetch格式

    fetch('url'+参数a, { method: "GET", body: json } .then(res => response.json()) .then(cons ...

  6. Eventbus 使用方法和原理分析

    对于 Eventbus ,相信很多 Android 小伙伴都用到过. 1.创建事件实体类 所谓的事件实体类,就是传递的事件,一个组件向另一个组件发送的信息可以储存在一个类中,该类就是一个事件,会被 E ...

  7. 同源策略Same-origin policy

     同源策略Same-origin policy 同源策略Same-origin policy是Web应用的一种安全基础策略.它规定同一源中,页面包含的脚本可以访问该源下的其他页面的数据.只有当网址中的 ...

  8. POJ 3041 Asteroids 二分图

    原题连接:http://poj.org/problem?id=3041 Asteroids Time Limit: 1000MS   Memory Limit: 65536K Total Submis ...

  9. 浅析PropertySource 基本使用

    目录 一.PropertySource 简介 二.@PropertySource与Environment读取配置文件 三.@PropertySource与@Value读取配置文件 四.@Propert ...

  10. Nessus虚拟机的几个问题解决办法

    1.使用ppp的校园网或者家庭宽带无法通过桥接上网. 这时要把这俩网卡变成NAT模式就行. 2.国外下载插件包(或者过慢). 我这里贡献个高速链接.base64,懂得自然懂. c3NyOi8vTkRj ...