leetcode236 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 p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself).”
Given the following binary tree: root = [3,5,1,6,2,0,8,null,null,7,4]
Example 1:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.
Example 2:
Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 4
Output: 5
Explanation: The LCA of nodes 5 and 4 is 5, since a node can be a descendant of itself according to the LCA definition.
"""
"""
本题两种解法。第一种非递归的方法
用queue来存储结点遍历
用dict{root, parent[root]}来存储每个结点的父亲结点
然后用一个set存储p的所有父辈结点
再遍历q的每个父亲结点查找是否再set中
如果找到即为p,q结点的最近公共祖先
"""
class TreeNode:
def __init__(self, x):
self.val = x
self.left = None
self.right = None class Solution1:
def lowestCommonAncestor(self, root, p, q):
queue = [root] #用来层次遍历
parent = {root: None} #用dict存储父亲结点
while queue:
node = queue.pop()
if node.left:
parent[node.left] = node #存父亲结点
queue.append(node.left) #入队
if node.right:
parent[node.right] = node
queue.append(node.right)
res = set() #set集合是一个无序不重复元素的序列
while p: #res=() 这是把res定义为tuple,tuple是只能查看的list
res.add(p) #将p的所有父辈结点放入set里
p = parent[p]
while q not in res: #q向上找到相同的父亲结点
q = parent[q]
return q """
第二种是递归写法:没有理解
传送门:https://blog.csdn.net/qq_17550379/article/details/95903394
树型问题首先考虑递归,对于每个树中的p和q只会有一下几种情况
1. p在左子树中,q在右子树中
2. q在左子树中,p在右子树中
3. p和q都在左子树中
4. p和q都在右子树中
对于第一种和第二种情况很简单,p和q的最近公共祖先就是root。
对于第三种情况我们只需递归向左子树找,第四种情况我们只需递归向右子树找。接着思考边界情况。
当p==root or q==root的时候,我们返回root即可(因为要找最近公共祖先,继续遍历的话,就不可能是其祖先了)。
那么这里就有一个迷惑人的地方了,请认真思考第一种和第二种情况下,左右子树的递归返回结果是什么?就是p和q。
""" class Solution2:
def lowestCommonAncestor(self, root, p, q):
if not root or root == p or root == q:
return root
left = self.lowestCommonAncestor(root.left, p, q)
right = self.lowestCommonAncestor(root.right, p, q)
if left and right:
return root
return left if left else right
leetcode236 Lowest Common Ancestor of a Binary Tree的更多相关文章
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree 236. Lowest Common Ancestor of a Binary Tree
https://www.cnblogs.com/grandyang/p/4641968.html http://www.cnblogs.com/grandyang/p/4640572.html 利用二 ...
- 【LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- Leetcode之236. Lowest Common Ancestor of a Binary Tree Medium
236. Lowest Common Ancestor of a Binary Tree Medium https://leetcode.com/problems/lowest-common-ance ...
- 88 Lowest Common Ancestor of a Binary Tree
原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...
- 【刷题-LeetCode】236. Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Tree Given a binary tree, find the lowest common ancestor (LCA) o ...
- [LeetCode] 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 Lowest Common Ancestor of a Binary Tree
原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...
- [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 ...
随机推荐
- POJ1797 Heavy Transportation (堆优化的Dijkstra变形)
Background Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand bus ...
- ES-倒排索引
一.什么事倒排索引 二.倒排索引为什么查询速度快 (1)ES全文检索的底层是使用倒排索引实现的 (2)倒排索引会将文档的内容进行关键词分词,可以使用关键词直接定位到文档的内容
- 激活win10企业版,亲测可用,(win7步骤相同,请自行测试)
其他版本我没试过,亲们可以尝试! win7神key win7神key1:2HYJ4-V71WM-BAF6Y-G2BTH-X8QOD win7神key2:9LM54-Z3LQ1-5IRAN-T4JNI- ...
- 【PAT甲级】1041 Be Unique (20 分)(多重集)
题意: 输入一个正整数N(<=1e5),接下来输入N个正整数.输出第一个独特的数(N个数中没有第二个和他相等的),如果没有这样的数就输出"None". AAAAAccepte ...
- Session服务器之Session复制!
全部运行在Tomcat下 第一台主机:192.168.200.131 安装nginx 修改hosts文件 [root@localhost ~]# vim /etc/hosts 192.168.200 ...
- Tomcat访问控制及站点部署(以WAR包形式上传)!(重点)
访问控制 首先安装好jdk以及apache-tomcat并能访问tomcat网页 点击server status了解服务状态会报403的错误 第一步:修改user.xml配置文件 [root@loca ...
- Python图文识别技术【入门必学】
Python图文识别技术分享 使用 tesseract-ORC 识别文字,识别率不算太高,需要自我训练 tessdata 数据,才能更精确的识别你想要让电脑认识出来的文字!ps:另外很多人在学习Pyt ...
- JUnit + Mockito 单元测试
原 JUnit + Mockito 单元测试(二) 2015年01月05日 17:26:02 sp42a 阅读数:60755 版权声明:本文为博主原创文章,未经博主允许不得转载. https://bl ...
- PTA的Python练习题(八)
从 第3章-15 统计一行文本的单词个数 继续 1. s = input() count=0 for c in s: if c==' ': count=count+1 if c=='.': break ...
- 【转】issue management in your test project
What is Issue Management? Issue Management is the process to make others aware of the problem and th ...