LeetCode Lowest Common Ancestor of a Binary Serach Tree
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.
这个题目的意思就是:让你找出两个结点最近的祖先,也就是和他们血缘关系最近的祖先
我的解法:深度优先搜索,再以这个结点,进行搜索,如果以这个结点进行搜索时,发现了我们要求的两个结点,则该结点就是这两个结点最近的 ,不过我的算法的时间复杂度很高。。。。
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void dfs(TreeNode *root, TreeNode *p, TreeNode *q,int &i)
{
if (root == NULL)return;
if (root == p || root == q)
++i;
dfs(root->left, p, q,i);
dfs(root->right, p, q,i);
}
TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
map<TreeNode*,int> visited;
stack<TreeNode*> sttn;
TreeNode * curNode;
sttn.push(root);
while (!sttn.empty())
{
curNode = sttn.top();
while(curNode->left != NULL&&visited[curNode->left] != )
{
curNode = curNode->left;
sttn.push(curNode);
}
if (curNode->right != NULL&&visited[curNode->right] != )
{
curNode = curNode->right;
sttn.push(curNode);
}
else
{
int cnt = ;
dfs(curNode, p, q,cnt);
if (cnt == )
return curNode;
visited[curNode] = ;
sttn.pop();
}
}
}
};
LeetCode Lowest Common Ancestor of a Binary Serach Tree的更多相关文章
- [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 ...
- LeetCode: Lowest Common Ancestor of a Binary Search Tree 解题报告
https://leetcode.com/submissions/detail/32662938/ Given a binary search tree (BST), find the lowest ...
- [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 ...
- LeetCode——Lowest Common Ancestor of a Binary Search Tree
Description: Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given no ...
- Python3解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 ...
- LeetCode Lowest Common Ancestor of a Binary Search Tree (LCA最近公共祖先)
题意: 给一棵二叉排序树,找p和q的LCA. 思路: 给的是BST(无相同节点),那么每个节点肯定大于左子树中的最大,小于右子树种的最小.根据这个特性,找LCA就简单多了. 分三种情况: (1)p和q ...
- [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 ...
- LeetCode Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- 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 ...
随机推荐
- VB的if和elseif
VB中if和elseif的用法是: if...then...elseif...then...else...endif 切记在then的后面不要加冒号,加了冒号出现else没有if的错误,因为加了冒号表 ...
- TKinter事件及绑定
Windows编程是基于消息的,绝大多数界面编程是基于事件的. 事件的绑定函数bind: 语法 :窗体对象.bind(事件类型,回调函数) 所谓的"回调函数",就是这个函数我们不用 ...
- PERCENT_RANK
1. percent_rank() over (order by .....) 返回某列或某列组合后每行的百分比排序 如下: with cte as( SELECT ROWNUM as n FRO ...
- 在数据库查询时解决大量in 关键字的方法
有时候在前台界面需要批量处理一些数据时,经常要用到update table set fields=value where keyid in ('1','2',....) 但如果这个数据量如果超过100 ...
- wcf 获取客户端 IP
http://stackoverflow.com/questions/3937773/wcf-security-using-client-ip-address var context = Operat ...
- SPOJ #442 Searching the Graph
Just CS rookie practice on DFS\BFS. But details should be taken care of: 1. Ruby implementation got ...
- mysql的主从配置以及主主配置
基础环境 系统:linuxmysql版本:5.5主服务器IP:192.168.1.101从服务器IP:192.168.1.102 1.主服务器(master)要打开二进制日志2.从服务器(slave) ...
- FastReport使用二——二维码
以下内容在FastReport Designer 中测试通过,如下图所示: 在使用FastReport Designer创建一维吗也就是一般普通的条码时,设置其Barcode属性为Code128 (建 ...
- Redis容量及使用规划(转)
在使用Redis过程中,我们发现了不少Redis不同于Memcached,也不同于MySQL的特征. (本文主要讨论Redis未启用VM支持情况) 1. Schema MySQL: 需事先设计Memc ...
- WindowsForm应用程序调用WebService
本文原创,如需转载,请标明源地址,谢谢合作!http://blog.csdn.net/sue_1989/article/details/6597078 本文的编写IDE为VSTS2008和.NET F ...