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

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

假设可能不会出现的话,也非常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. scrapy的调试方法

    Parse命令,Scrapy shell,logging 一 Parse命令 检查spider输出的最基本方法是使用Parse命令.这能让你在函数层上检查spider哥哥部分的效果,其十分灵活并且已用 ...

  2. 【CF676D】Theseus and labyrinth(BFS,最短路)

    题意:给定一张N*M的地图,每一格都是一个房间,房间之间有门.每个房间可能有四个门,例如>代表右边只有一个门在右边即只能向右走,L代表左边没有门只能除了左其他都可以走等等.现在给出起点和终点,每 ...

  3. angular杂谈

    <element ng-include="filename" onload="expression" autoscroll="expressio ...

  4. es6总结(十)--class

  5. 标准C程序设计七---70

    Linux应用             编程深入            语言编程 标准C程序设计七---经典C11程序设计    以下内容为阅读:    <标准C程序设计>(第7版) 作者 ...

  6. AC日记——dispatching bzoj 2809

    2809: [Apio2012]dispatching Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 3290  Solved: 1740[Submi ...

  7. 重复造轮子之RSA算法(一) 大素数生成

    出于无聊, 打算从头实现一遍RSA算法 第一步, 大素数生成 Java的BigInteger里, 有个现成的方法 public static BigInteger probablePrime(int ...

  8. Codeforces Gym101502 K.Malek and Summer Semester

    K. Malek and Summer Semester   time limit per test 1.0 s memory limit per test 256 MB input standard ...

  9. Codeforces 599E Sandy and Nuts(状压DP)

    题目链接 Sandy and Nuts 题意大概就是给出限制条件求出在该限制条件下树的种数. #include <bits/stdc++.h> using namespace std; # ...

  10. 第3章 Spring Boot 入门指南

    Part II. 入门指南 如果你刚刚开始使用Spring Boot,这是你的一部分内容! 在这里我们将会回答一些基本的“what?”, “how?” 和 “why?”的问题. 在这里你会找到一个详细 ...