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 ...
随机推荐
- 最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...
- comet4j文档
Comet4J配置参数表 <!--Comet4J配置 --> <listener> <description>Comet4J容器侦听</description ...
- C#操作xml SelectNodes,SelectSingleNode总是返回NULL 与 xPath 介绍
一. SelectNodes,SelectSingleNode总是返回NULL 下面以一个简单的xml为例: <?xml version="1.0"?> <mes ...
- 控制台程序实现利用CRM组织服务和SqlConnection对数据库中数据的增删改查操作
一.首先新建一个控制台程序.命名为TestCol. 二.打开App.config在里面加入,数据库和CRM连接字符串 <connectionStrings> <add name=&q ...
- Jfinal基础学习(一)
我负责的项目都是Jfinal的了,有必要写一些学习知识,记录下来. 1.PropKit.use("config.txt", "UTF-8"); PropKit ...
- 图片上传,图片剪切jquery.imgareaselect
---恢复内容开始--- <%@ page language="java" contentType="text/html; charset=UTF-8" ...
- 剑指offer系列24---数组中重复的数字
* [24] * [题目]在一个长度为n的数组里的所有数字都在0到n-1的范围内. * 数组中某些数字是重复的,但不知道有几个数字是重复的. * 也不知道每个数字重复几次. * 请找出数组中任意一个重 ...
- grep,sed,cut,awk,join个性特点
grep 从数据文件中查询/提取出含有特定关键字的行. sed 主要用于对数据文件中特定字符串的替换处理. cut 按照指定的分隔符(-d)剪下选定的列(-f num)或者字符(-c)的内容. awk ...
- WeX5和BeX5比较
http://wex5.com/cn/wex5和bex5比较/ WeX5和BeX5比较 许多对WeX5和BeX5略有了解得人都知道,WeX5和BeX5是完全共用前端框架技术的.但是WeX5和BeX5是 ...
- 【MySQL】unique列插入重复值解决方案
当在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ON DUPLICATE KEY UPDATE子句跳过INSERT.中断操作或者更新旧 ...