关闭

【Leetcode】Lowest Common Ancestor of a Binary Search Tree

262人阅读 评论(0) 收藏 举报
分类:

题目链接:https://leetcode.com/problems/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 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.

思路:

因为是二分查找树,p/q的最低公共祖先结点一定p<root<q,所以可以根据这个关系来判断。

算法:

	public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
		if (root.val > p.val && root.val < q.val) { //p.val<root.val<q.val,则root是p/q的父结点
			return root;
		}
		if (root.val > p.val && root.val > q.val) {//root.val>p.val,>q.val,则p/q最低祖先结点在root左子树
			return lowestCommonAncestor(root.left, p, q);
		}
		if (root.val < p.val && root.val < q.val) {
			return lowestCommonAncestor(root.right, p, q);
		}
		return root;
	}


0
0
查看评论

leetcode 235: Lowest Common Ancestor of a Binary Search Tree

leetcode 235: Lowest Common Ancestor of a Binary Search Tree python, java, c++
  • xudli
  • xudli
  • 2015-07-11 06:34
  • 10633

[Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree

[Java]LeetCode235 Lowest Common Ancestor of a Binary Search Tree
  • fumier
  • fumier
  • 2015-08-22 21:31
  • 735

235. Lowest Common Ancestor of a Binary Search Tree [easy] (Python)

题目链接https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-search-tree/题目原文 Given a binary search tree (BST), find the lowest common ances...
  • coder_orz
  • coder_orz
  • 2016-05-25 15:26
  • 888

[LeetCode 236] 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 to the definition of LCA on Wikipedia: “T...
  • sbitswc
  • sbitswc
  • 2015-08-25 13:31
  • 2438

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 BST. According to the definition of LCA on Wikipe...
  • sunao2002002
  • sunao2002002
  • 2015-07-27 18:41
  • 2164

[Java]Leetcode236 Lowest Common Ancestor of a Binary Tree

求二叉树的公共父结点
  • fumier
  • fumier
  • 2015-08-23 08:58
  • 853

LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树的最低公共祖先)

原题网址:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the lowest common ancestor (LCA) of two given ...
  • jmspan
  • jmspan
  • 2016-04-06 06:43
  • 588

leetcode 236. Lowest Common Ancestor of a Binary Tree-二叉树共同祖先|深度遍历|递归|非递归

原题链接:236. Lowest Common Ancestor of a Binary Tree 【思路-Java】dfs|递归实现 本题是二叉树的深度遍历的典型应用,基础还是二叉树的遍历。 以根节点为起点,往左右分支上寻找,如果找到了 p 或 q 节点,则返回该节点。否则,继续向叶子节点寻...
  • happyaaaaaaaaaaa
  • happyaaaaaaaaaaa
  • 2016-06-11 11:44
  • 688

leetcode 236: Lowest Common Ancestor of a Binary Tree

leetcode 236: Lowest Common Ancestor of a Binary Tree PYTHON, JAVA, C++
  • xudli
  • xudli
  • 2015-07-14 02:35
  • 6343

P2P点播系统性能优化(3): Tracker服务器选择最优节点

在集中式的P2P网络系统中,Tracker服务器的主要功能是提供最优节点检索功能 ;Tracker服务器的最优节点选择算法如下:               ...
  • checo_chua
  • checo_chua
  • 2011-08-17 14:52
  • 666
    个人资料
    • 访问:568325次
    • 积分:8669
    • 等级:
    • 排名:第2679名
    • 原创:305篇
    • 转载:6篇
    • 译文:0篇
    • 评论:37条
    博客专栏
    文章分类
    最新评论