[Leetcode] Lowest Common Ancestor of a Binary Tree 最小公共祖先
来源:互联网 发布:订货软件破解版 编辑:程序博客网 时间:2024/05/29 15:40
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.
二分法
复杂度
时间 O(h) 空间 O(h) 递归栈空间
思路
对于二叉搜索树,公共祖先的值一定大于等于较小的节点,小于等于较大的节点。换言之,在遍历树的时候,如果当前结点大于两个节点,则结果在当前结点的左子树里,如果当前结点小于两个节点,则结果在当前节点的右子树里。
代码
public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { if(root.val > p.val && root.val > q.val) return lowestCommonAncestor(root.left, p, q); if(root.val < p.val && root.val < q.val) return lowestCommonAncestor(root.right, p, q); return root; }}
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: “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).”
_______3______ / \ ___5__ ___1__ / \ / \ 6 _2 0 8 / \ 7 4
For example, the lowest common ancestor (LCA) of nodes 5 and 1 is 3. Another example is LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
深度优先标记
复杂度
时间 O(h) 空间 O(h) 递归栈空间
思路
我们可以用深度优先搜索,从叶子节点向上,标记子树中出现目标节点的情况。如果子树中有目标节点,标记为那个目标节点,如果没有,标记为null。显然,如果左子树、右子树都有标记,说明就已经找到最小公共祖先了。如果在根节点为p的左右子树中找p、q的公共祖先,则必定是p本身。
换个角度,可以这么想:如果一个节点左子树有两个目标节点中的一个,右子树没有,那这个节点肯定不是最小公共祖先。如果一个节点右子树有两个目标节点中的一个,左子树没有,那这个节点肯定也不是最小公共祖先。只有一个节点正好左子树有,右子树也有的时候,才是最小公共祖先。
代码
public class Solution { public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) { //发现目标节点则通过返回值标记该子树发现了某个目标结点 if(root == null || root == p || root == q) return root; //查看左子树中是否有目标结点,没有为null TreeNode left = lowestCommonAncestor(root.left, p, q); //查看右子树是否有目标节点,没有为null TreeNode right = lowestCommonAncestor(root.right, p, q); //都不为空,说明做右子树都有目标结点,则公共祖先就是本身 if(left!=null&&right!=null) return root; //如果发现了目标节点,则继续向上标记为该目标节点 return left == null ? right : left; }}
- [Leetcode] Lowest Common Ancestor of a Binary Tree 最小公共祖先
- Leetcode-236. Lowest Common Ancestor of a Binary Tree 最小公共祖先
- leetcode之二叉树类之最小公共祖先-----236/235. Lowest Common Ancestor of a Binary/Binary Search Tree
- 最低公共祖先Lowest Common Ancestor of a Binary Tree
- LeetCode 235 Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最小公共祖先)
- Lowest Common Ancestor of a Binary Search Tree(二叉树最小公共祖先)
- LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树的最低公共祖先)
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 最近公共祖先 + BST
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree(二叉搜索树的最低公共祖先)
- Lowest Common Ancestor of a Binary Search Tree 递归寻找二叉查找树公共祖先
- Lowest Common Ancestor of a Binary Tree 二叉树的公共祖先
- 236. Lowest Common Ancestor of a Binary Tree最长公共祖先
- 【二叉树】最近公共祖先Lowest Common Ancestor of a Binary Tree
- 【二叉树】最近公共祖先【235. Lowest Common Ancestor of a Binary Search Tree】
- leetcode 236. Lowest Common Ancestor of a Binary Tree 最近公告祖先LCA + 二叉树
- Lowest Common Ancestor of a Binary Tree 二叉树中的最小公共父节点
- 二叉搜索树(BST)的最近公共祖先(LCA)问题(Lowest Common Ancestor of a Binary Tree)
- [Java]寻找最低公共祖先(排序二叉树)Lowest Common Ancestor of a Binary Search Tree
- oracle execute immediate 权限不足ora-01031
- 编写高质量的JavaScript代码
- 基于Mybatis分页插件PageHelper实现分页功能
- 冒泡排序(面向对象)
- PHP连接webservice调试相关
- [Leetcode] Lowest Common Ancestor of a Binary Tree 最小公共祖先
- CreateThread和_beginthread区别及使用
- RN布局(三)——伸缩项目的属性
- mysql在java中处理update语句使用了表别名,不起作用
- 二叉树的基本操作--递归
- 内存恶鬼drawRect
- SwipeRefreshLayout、ScrollView、RecyclerView嵌套使用
- 面试常用排序算法
- RN布局(四)——在React Native中使用flexbox