Lowest Common Ancestor of a Binary Search Tree
2015-07-11 15:14
246 查看
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).”
For example, the lowest common ancestor (LCA) of nodes
解题思路:
CC150里4.7,但是原题是任意二叉树,这里更为简单,被限定为二叉搜索树。
根据BST的性质,root.left.val < root.val < root.right.val,并且所有子树也都是BST。我们的思路就变成判断p和q到底是在root的哪边。
1. 如果都小于root.val,一定同时在左侧,root不是lca,递归搜索左子树。
2. 如果都大于root.val,递归搜索右子树。
3. 否则,分别在root两侧,root就是lca。
上面算法的时间复杂度是O(logn),等于树的高度。下面的算法可以用在任意二叉树,不一定是BST。
仅在root左右子树都分别找到p和q的情况下返回root,说明root是p和q的lca。否则找不到的返回null,找到的一侧返回p或者q。
上面的解法时间复杂度为O(n)。
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
2and
8is
6. Another example is LCA of nodes
2and
4is
2, since a node can be a descendant of itself according to the LCA definition.
解题思路:
CC150里4.7,但是原题是任意二叉树,这里更为简单,被限定为二叉搜索树。
根据BST的性质,root.left.val < root.val < root.right.val,并且所有子树也都是BST。我们的思路就变成判断p和q到底是在root的哪边。
1. 如果都小于root.val,一定同时在左侧,root不是lca,递归搜索左子树。
2. 如果都大于root.val,递归搜索右子树。
3. 否则,分别在root两侧,root就是lca。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null) { return null; } if(root == p || root == q) { return root; } TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if(left == null || right == null) { return left == null ? right : left; } return root; } }
上面算法的时间复杂度是O(logn),等于树的高度。下面的算法可以用在任意二叉树,不一定是BST。
仅在root左右子树都分别找到p和q的情况下返回root,说明root是p和q的lca。否则找不到的返回null,找到的一侧返回p或者q。
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root == null) { return null; } if(root == p || root == q) { return root; } TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); if(left == null || right == null) { return left == null ? right : left; } return root; } }
上面的解法时间复杂度为O(n)。
相关文章推荐
- ASP玩转微信公众平台自定义接口(1)—接口申请
- Java并发编程:volatile关键字解析
- C++ 设计三法则 二法则
- A simple stone game - HDU 2486 K倍递减博弈
- win7上搭建loadrunner11测试环境
- java面向对象思想学习
- Andorid的事件处理
- Ruby 元编程
- PHP include语句和require语句
- ecshop二次开发 给商品添加自定义字段
- python使用clear方法清除字典内全部数据实例
- C9_作业
- 使用Samba实现Linux与Windows文件共享实践
- 环境配置
- Minimum Sum - HDU 3473 划分树
- 日历节气显示不正常2015年1月6号是小寒,而现在是显示2015年1月5号是小寒
- WCF中修改接口或方法名称而不影响客户端程序
- 记一次系统稳定性问题的分析处理过程(因CallContext使用不当而造成bug)
- PageObject
- bzoj 1185 旋转卡壳 最小矩形覆盖