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 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).”
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,也就是两个节点的最低公共祖先。比如上图的二叉树,节点2和8的LCA是6,节点2和4的LCA是2。
思路:观察给定的二叉树可知,二叉树中任何节点:左节点的值 < 根节点的值 < 右节点的值。根据这个性质,可以做出如下判断:
- 如果p、q都比根节点小,则在左子树中递归查找最低公共祖先节点。
- 如果p、q都比根节点大,则在右子树中递归查找最低公共祖先节点。
- 如果p、q一个比根节点大,一个比根节点小,或者有一个等于根节点,则根节点即为最低公共祖先节点。
Language : cpp
递归方法:
/*** 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:TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {if (p->val < root->val && root->val > q->val)return lowestCommonAncestor(root->left, p, q);else if (p->val > root->val && root->val < q->val)return lowestCommonAncestor(root->right, p, q);elsereturn root;}
};
Language : python
递归方法:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution(object):def lowestCommonAncestor(self, root, p, q):""":type root: TreeNode:type p: TreeNode:type q: TreeNode:rtype: TreeNode"""if p.val < root.val > q.val:return self.lowestCommonAncestor(root.left, p, q)elif p.val > root.val < q.val:return self.lowestCommonAncestor(root.right, p, q)else:return root
迭代方法:
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = Noneclass Solution(object):def lowestCommonAncestor(self, root, p, q):""":type root: TreeNode:type p: TreeNode:type q: TreeNode:rtype: TreeNode"""while True:if p.val < root.val > q.val:root = root.leftelif p.val > root.val < q.val:root = root.rightelse:return root
代码获取: https://github.com/Jack-Cherish/LeetCode
235. Lowest Common Ancestor of a Binary Search Tree(Tree-Easy)相关推荐
- 235 Lowest Common Ancestor of a Binary Search Tree
题目 235 Lowest Common Ancestor of a Binary Search Tree 因为是binary search tree,因此利用没个节点的值进行二分查找即可复杂度O(h ...
- [LeetCode]235.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 ...
- 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 ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree | 235. 二叉搜索树的最近公共祖先(哈希表)
题目 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/ 题解 哈希表解法思路来自左程云< ...
- 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 ...
- [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 ...
- 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
题目:Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the ...
- LeetCode:235. 二叉搜索树的最近公共祖先(Lowest Common Ancestor of a Binary Search Tree)
二叉搜索树性质: 1.任意节点node,其左子树中的val不大于node.val,其右子树中的val不小于node.val. 2.不同的二叉搜索树可以代表同一组值的集合 3.二叉搜索树的基本操作和树的 ...
最新文章
- 什么是闭包?变量作用域和闭包。
- JavaScript 精粹 基础 进阶(4)对象
- .net core快速上手
- c++中的void指针和const指针
- python推荐哪个系统好_什么是好的推荐系统?
- boost::geometry::closeable_view用法的测试程序
- ES6标准入门(第二版)pdf
- 使用layer.tips实现鼠标悬浮时触发事件提示消息实现
- 一天完成一点,进度太慢了啊
- 查看其他计算机的共享资源,NetResView (共享资源查看)
- 源码解析——消息机制
- python中质数的表达方式_python求质数的3种方法
- 《深入浅出MFC》第三章 MFC六大关键技术之仿真
- Python 遗传算法路径规划
- vscode如何调整字体大小
- Python之面向对象-类与 类之间的关系
- 人工智能的示例——八皇后问题
- flask python 上传图片或头像
- Oracle数据库常用语句使用记录
- windows11+wsl+clion调试srs