题目

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

According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w as descendants (where we allow a node to be a descendant of itself).”

           6/         \2           8/    \      /     \0      4    7       9/  \3    5

For example, the lowest common ancestor (LCA) of nodes 2 and 8 is 6. Another example is LCA of nodes 2 and 4 is 2, since a node can be a descendant of itself according to the LCA definition.

思路

[算法系列之三十一]近期公共祖先(LCA)

代码

/*---------------------------------------
*   日期:2015-07-14
*   作者:SJF0115
*   题目: 235.Lowest Common Ancestor of a Binary Search Tree
*   网址:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/
*   结果:AC
*   来源:LeetCode
*   博客:
-----------------------------------------*/
#include <iostream>
#include <vector>
using namespace std;struct TreeNode{int val;TreeNode *left;TreeNode *right;TreeNode(int x):val(x),left(NULL),right(NULL){}
};class Solution {
public:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if(root == nullptr || p == nullptr || q == nullptr){return nullptr;}//ifreturn helper(root,p,q);}
private:TreeNode* helper(TreeNode* root,TreeNode* p,TreeNode* q){if(root == nullptr || p == nullptr || q == nullptr){return nullptr;}//ifint pVal = p->val;int qVal = q->val;int rootVal = root->val;// 分居两側if((pVal <= rootVal && qVal >= rootVal) || (pVal >= rootVal && qVal <= rootVal)){return root;}//if// 左側if(pVal < rootVal && qVal < rootVal){return helper(root->left,p,q);}//if// 右側if(pVal > rootVal && qVal > rootVal){return helper(root->right,p,q);}//if}
};int main(){Solution s;TreeNode* root = new TreeNode(6);TreeNode* node1 = new TreeNode(0);TreeNode* node2 = new TreeNode(9);TreeNode* node3 = new TreeNode(2);TreeNode* node4 = new TreeNode(3);TreeNode* node5 = new TreeNode(4);TreeNode* node6 = new TreeNode(5);TreeNode* node7 = new TreeNode(7);TreeNode* node8 = new TreeNode(8);root->left = node3;root->right = node8;node3->left = node1;node3->right = node5;node5->left = node4;node5->right = node6;node8->left = node7;node8->right = node2;TreeNode* node = s.lowestCommonAncestor(root,node3,node4);if(node != nullptr){cout<<node->val<<endl;}//ifelse{cout<<"nullptr"<<endl;}//elsereturn 0;
}

执行时间

[LeetCode]235.Lowest Common Ancestor of a Binary Search Tree相关推荐

  1. leetcode 235. Lowest Common Ancestor of a Binary Search Tree | 235. 二叉搜索树的最近公共祖先(哈希表)

    题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 题解 哈希表解法思路来自左程云< ...

  2. 235 Lowest Common Ancestor of a Binary Search Tree

    题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...

  3. 235. Lowest Common Ancestor of a Binary Search Tree(Tree-Easy)

    转载请注明作者和出处: http://blog.csdn.net/c406495762 Given a binary search tree (BST), find the lowest common ...

  4. Leet Code OJ 235. Lowest Common Ancestor of a Binary Search Tree [Difficulty: Easy]

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

  5. Lowest Common Ancestor of a Binary Search Tree a Binary Tree

    235. Lowest Common Ancestor of a Binary Search Tree 题目链接:https://leetcode.com/problems/lowest-common ...

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

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

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

  9. LeetCode:235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree)

    二叉搜索树性质: 1.任意节点node,其左子树中的val不大于node.val,其右子树中的val不小于node.val. 2.不同的二叉搜索树可以代表同一组值的集合 3.二叉搜索树的基本操作和树的 ...

最新文章

  1. C# 中字符串string和字节数组byte[]的转换,16 进制字符串转 int的方法
  2. 彻底剖析激光-视觉-IMU-GPS融合SLAM算法:理论推导、代码讲解和实战
  3. 世界银行拨款2293万美元支持印度并网屋顶太阳能
  4. 【Android 应用开发】Android 开发错误集锦
  5. 图的定义存储和遍历(一级)
  6. java正则表达式 匹配()_学习Java正则表达式(匹配、替换、查找)
  7. 96. 不同的二叉搜索树
  8. [转载] C#面向对象设计模式纵横谈——16 Interpreter解释器模式
  9. DPDK内存篇(一): 基本概念
  10. 常用数据类型使用转换详解
  11. Linux命令整理 - 通用版
  12. npoi 未将对象引用设置到对象的实例_带你探索JVM的对象
  13. 计算机中专生实习单位的鉴定表,中专生的实习自我鉴定(精选5篇)
  14. android 全局剪贴板,Android剪贴板详解
  15. 使用Arduino开发ESP32(18):使用Preferences保存数据
  16. Kali BeEF MSF的使用
  17. 在android view中写坦克大战
  18. 不同厂商手机系统日志抓取方法
  19. Java并发HashMap报错ConcurrentModificationException解决方案
  20. 23计算机考研复习规划和经验分享

热门文章

  1. mysql 1418 存储过程_MySQL自定义函数 1418报错
  2. sample等价是什么错误_一个复制粘贴引发的有趣小错误及思考
  3. Java面试题:高并发环境下,jdk7 HashMap可能出现的致命问题。注意:是在jdk7与及以下版本
  4. golang channel的一些总结
  5. Dubbo源码分析系列之-深入Dubbo扩展机制
  6. Hadoop入门基础教程 Hadoop之伪分布式环境搭建
  7. input属性disabled和readonly的区别
  8. linux性能监控sar命令详解
  9. Laravel源码分析之Session
  10. Dubbo服务端暴露全流程