Lowest Common Ancestor of a Binary Tree leetcode
来源:互联网 发布:生物医学大数据 编辑:程序博客网 时间:2024/06/07 09:55
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.
分析:
给定一棵二叉树,找到两个节点的最近公共父节点(LCA)。
最近公共祖先是两个节点的公共的祖先节点且具有最大深度。
这道题可以用分治法完成,
分治算法的基本思想是将一个规模为N的问题分解为K个规模较小的子问题,这些子问题相互独立且与原问题性质相同。
求出子问题的解,就可得到原问题的解。即一种分目标完成程序算法,简单问题可用二分法完成。
当我们求解某些问题时,由于这些问题要处理的数据相当多,或求解过程相当复杂,使得直接求解法在时间上相当长,或者根本无法直接求出。
对于这类问题,我们往往先把它分解成几个子问题,找到求出这几个子问题的解法后,再找到合适的方法,把它们组合成求整个问题的解法。
如果这些子问题还较大,难以解决,可以再把它们分成几个更小的子问题,以此类推,直至可以直接求出解为止。
实际处理在上,那就是从上向下,因为先处理再递归,
实际处理在下,那就是从下向上,因为先递归再处理,处理是等到递归返回之后做的。
/////
如果一个节点左子树有两个目标节点中的一个,右子树没有,那这个节点肯定不是最小公共祖先。
如果一个节点右子树有两个目标节点中的一个,左子树没有,那这个节点肯定也不是最小公共祖先。
只有一个节点正好左子树有,右子树也有的时候,才是最小公共祖先。
/** * 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) { // 在root为根的二叉树中找A,B的LCA: // 如果找到了就返回这个LCA // 如果只碰到A,就返回A // 如果只碰到B,就返回B // 如果都没有,就返回null if (root == null) { return null; } if (root == p || root == q) { return root; } // Divide TreeNode left = lowestCommonAncestor(root.left, p, q); TreeNode right = lowestCommonAncestor(root.right, p, q); // Conquer if(left != null && right != null) { return root; } if (left != null) { return left; } if (right != null) { return right; } return null; }}
根空返空,
根等p或根等q,返根
左子树递归,
右子树递归,
左非空且右非空返根,
左非空返左,
右非空返右,
返空。
- leetcode 235: Lowest Common Ancestor of a Binary Search Tree
- leetcode - Lowest Common Ancestor of a Binary Tree
- LeetCode 235. Lowest Common Ancestor of a Binary Search Tree
- leetcode: Lowest Common Ancestor of a Binary Search Tree
- [leetcode][dfs] Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 235.Lowest Common Ancestor of a Binary Search Tree
- [leetcode] 236.Lowest Common Ancestor of a Binary Tree
- [LeetCode]236.Lowest Common Ancestor of a Binary Tree
- 【LeetCode】235 Lowest Common Ancestor of a Binary Search Tree
- 【LeetCode】236 Lowest Common Ancestor of a Binary Tree
- leetcode 236: Lowest Common Ancestor of a Binary Tree
- [LeetCode]235.Lowest Common Ancestor of a Binary Search Tree
- LeetCode OJ 之 Lowest Common Ancestor of a Binary Tree
- leetcode 235 Lowest Common Ancestor of a Binary Search Tree
- [leetcode] Lowest Common Ancestor of a Binary Search Tree
- [leetcode] Lowest Common Ancestor of a Binary Tree
- Leetcode 236 Lowest Common Ancestor of a Binary Tree
- LeetCode Lowest Common Ancestor of a Binary Search Tree
- Android自学初步——自定义view(2)————UI绘制圆与ViewPager互动
- 排序3和字符串1(6道)
- 关于接口的定义和使用,几点需要注意的问题
- #2016全球微服务架构高峰论坛
- JavaEE实战——Tomcat、web程序结构、Http协议、HttpUrlConnection
- Lowest Common Ancestor of a Binary Tree leetcode
- windows下jsp+mysql网站环境配置
- R语言apply族函数详解
- 近几年愿望单
- hdu 5842 Lweb and String(水题)
- MyEclipse+Struts+Hibernate+Mysql开发环境配置
- HDU 5835 Danganronpa(贪心)
- Centos 7 中文显示乱码解决办法
- javascript中的回调函数【理解记忆】