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树的最小公共祖先
如果一个节点的左右子树上分别有两个节点,那么这棵树是祖先,但是不一定是最小的,但是从下边开始判断,找到后一直返回到上边就是最小的. 如果一个节点的左右子树上只有一个子树上遍历到了节点,那么那个子树可能 ...
随机推荐
- [cocos2dx笔记006]流格式日志
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zdhsoft/article/details/36001945 在cocos2dx 2.2.2版本号 ...
- 解决linux下python多版本兼容问题?
环境:CentOS 7(7下默认安装python2.7) Pyhon3在CentOS中需要依赖一些其他的包,我们一次性用yum安装一下: yum install zlib-devel bzip2-de ...
- Oracle DG强制激活 备库
在实际运营环境中,我们经常碰到类似这样的需求,譬如想不影响现网业务评估DB补丁在现网环境中运行的时间,或者是想在做DB切换前想连接Standby DB做实际业务运行的测试,如果在9i版本的时候,想做到 ...
- Android bluetooth介绍
Android bluetooth介绍(一):基本概念及硬件接口Android bluetooth介绍(二): android 蓝牙代码架构及其uart 到rfcomm流程Android blueto ...
- Python3.x:百分比数转小数
Python3.x:百分比数转小数 def change_percent(num): zfflag = "" if "+" in num: num = num. ...
- qss 样式不生效--注释不能嵌套
qss 兼容 css 调qt样式的时候出现一个很奇怪的问题 删掉注释掉的内容 样式正常,不删注释 ,注释后面的样式全部失效.有点困惑的是: 感觉很困惑,后来发现里面有行中文注释,删掉中文注释就行了,以 ...
- cisco笔记
交换机 show cdp neighbors 显示邻居信息 路由 show ip interface brief 显示接口ip
- Java web应用中的常见字符编码问题的解决方法
以下是 Java Web应用的常见编码问题 1. html页面的编码 在web应用中,通常浏览器会根据http header: Content-type的值来决定用什么encoding, 比如遇到Co ...
- MySql 查询数据库中所有表名以及对比分布式库中字段和表的不同
查询数据库中所有表名select table_name from information_schema.tables where table_schema='数据库名' and table_type= ...
- select 下拉框 设置值
function setSelectOption(objSelect, targetValue){ if(objSelect){ var options = objSelect.options; if ...