LeetCode OJ: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.
找最近的公共祖先,仔细想一想,其实l与r的最小的公共祖先c满足一定条件,那就是l以及r一定在c的左右分支上,不可能都是左或右分支的。否则一定就不是最近的公共祖先,
代码如下,用递归写出来还是比较简单易懂的。
/**
* 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 (root == NULL) return NULL; //无其他节点,直接返回
if (root == p || root == q) return root;
TreeNode * leftNode = lowestCommonAncestor(root->left, p, q);
TreeNode * rightNode = lowestCommonAncestor(root->right, p, q);
if (leftNode && rightNode) return root; //找到LCA,返回LCA
return leftNode ? leftNode : rightNode;
}
};
还有一种方法是遍历tree,然后找出到达p以及q分别的路径,找到路径之后,遍历两条路径,出现分叉的第一个点就是p与q的LCA。具体代码先不贴了 比较麻烦。
java版本的如下所示,方法相同:
/**
* 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 leftNode = lowestCommonAncestor(root.left, p, q);
TreeNode rightNode = lowestCommonAncestor(root.right, p, q);
if(leftNode != null && rightNode != null)
return root;
if(leftNode!=null) return leftNode;
if(rightNode != null) return rightNode;
return null;
}
}
LeetCode OJ:Lowest Common Ancestor of a Binary Tree(最近公共祖先)的更多相关文章
- [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 ...
- [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 ...
- leetcode@ [236] Lowest Common Ancestor of a Binary Tree(Tree)
https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Given a binary tree, find the ...
- 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 ...
- (medium)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 ...
- [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. Accordi ...
- [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 ...
- LeetCode 236 Lowest Common Ancestor of a Binary Tree 二叉树两个子节点的最低公共父节点
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- Java for LeetCode 236 Lowest Common Ancestor of a Binary Tree
解题思路一: DFS到每个节点的路径,根据路径算出LCA: public class Solution { public TreeNode lowestCommonAncestor(TreeNode ...
- [leetcode]236. Lowest Common Ancestor of a Binary Tree树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...
随机推荐
- Hibernate简单配置
1.配置构建路径,加载用户库,hibernate4.3.8 MySQL-Driver 2.写User.java 纯POJO+持久化注解=PO @Entity @Table(name=&quo ...
- SQL LEFT JOIN
SQL LEFT JOIN 关键字 SQL LEFT JOIN 关键字 LEFT JOIN 关键字会从左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中没有 ...
- 吴超老师课程---ZooKeeper介绍和集群安装
1.ZooKeeper 1.1 zk可以用来保证数据在zk集群之间的数据的事务性一致.2.如何搭建ZooKeeper服务器集群 2.1 zk服务器集群规模不小于3个节点,要求各服务器之间系 ...
- 使用反射实现 webdriver page 类
这个类的目的是为了简化page类的实例化,只需要定义public page成员变量 然后再 启动driver后 通过反射实例化page 后面可以直接点出page实例 package crazy.sel ...
- hibernatetemplate find条件查询方法
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...
- springmvc 需要用到的核心jar包
aopbeanscontextcoreexpressionwebwebmvc
- iptables打开22,80,8080,3306等端口
systemctl stop firewalld systemctl mask firewalld Then, install the iptables-services package: yum i ...
- URAL 2081 Faulty dial
题目: Faulty dial Pavel has not played ACM for ages, nor does he train teams, nor prepare problems. Th ...
- CodeForces - 451E Devu and Flowers (容斥+卢卡斯)
题意:有N个盒子,每个盒子里有fi 朵花,求从这N个盒子中取s朵花的方案数.两种方法不同当且仅当两种方案里至少有一个盒子取出的花的数目不同. 分析:对 有k个盒子取出的数目超过了其中的花朵数,那么此时 ...
- nohup- Shell后台运行
&方式: Unix/Linux下一般想让某个程序在后台运行,很多都是使用 & 在程序结尾来让程序自动运行.比如我们要运行mysql在后台: /usr/local/my ...