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 ...
随机推荐
- TKinter之文本域与多窗口
用tkinter做出一个文本框出来,用于写字 代码很简单: #!/usr/bin/env python # _*_ coding:utf-8 _*_ from Tkinter import * roo ...
- oracle定时任务JOB
在jobs上点新建 what值:statisticsToDay; 这个是存储过程的名字间隔:sysdate+1/24 表示每个小时运行一次 1:每分钟执行 Interval ...
- linux shell 整理收集(不断更新)
1)主从复制延时判断 (转 http://www.cnblogs.com/gomysql/p/3862018.html) 说明: 不要通过Seconds_Behind_Master去判断,该值表示sl ...
- Android 查看內存使用
一.使用dumpsys meminfo命令 1.使用dumpsys meminfo查看内存使用情况 2.过滤某个进程可以使用 dumpsys meminfo | grep -i phone 二,使用t ...
- 实现手电筒Flash Light 关键代码
实现手电筒Flash Light 关键代码 实现Flash的逻辑 view.setOnClickListener(new OnClickListener() { @Override public vo ...
- 在VS2008环境下的C++异常处理
在写DAServer的过程中,一直在重视报文逻辑处理,却没有认认真真地去思考异常处理的问题.曾经我发现我在所有的报文处理函数中均没有考虑报文长度的问题,让我内心不由地捏了一把冷汗.我在新增的故障录波及 ...
- windows p12(pfx)个人证书安装过程
证书库个人证书存储区为其中的每个证书维护一个属性CERT_KEY_PROV_INFO_PROP_ID,该属性指定了证书对应的密钥容器的相关信息,包括密钥容器名,CSP名称,CSP类型,密钥用途,以及C ...
- AngularJs中的服务
一.angularJs中的简单服务应用 下面的例子让我们明白在AngularJs中如何去调用文件中的数据,从而将文件中的数据显示在页面上;改变url的地址,也可以去调用后台接口. 实例: <!D ...
- Windows2008 Patching(打补丁)
我们都知道Windows的服务器都需要打补丁的,要不然漏洞那个叫多啊.Windows的系列服务器打补丁无非就是两种方法: 1. 通过Internet打补丁: Go to control Panel-& ...
- erlang和java通信
连接在 https://guts.me/2014/07/27/erlang-communicates-with-java/ 代码在 https://github.com/mingshun/jinter ...