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 ...
随机推荐
- 洛谷P1991 无线通讯网(最小生成树性质+连通块)
题目描述 国防部计划用无线网络连接若干个边防哨所.2 种不同的通讯技术用来搭建无线网络: 每个边防哨所都要配备无线电收发器:有一些哨所还可以增配卫星电话. 任意两个配备了一条卫星电话线路的哨所(两边都 ...
- leetcode: 0204 完成的
目录 大纲:0204 完成的 notes ✅1051 高度检查器 ✅ 728 自除数 brute c解答: java switch 语句 java api: array 直接有 length 属性 , ...
- 通过LAMP部署phpMyAdmin、wordpress(https)、discuz
1.安装启动LAMP 安装环境: CentOS Linux release 7.5.1804 安装包: # yum -y install httpd php php-mysql mariadb-ser ...
- 与英特尔分道扬镳,苹果的5G业务掉队了吗?
5G概念已经大热,越来越多的厂商推出相关产品,中国骄傲之华为不仅在5G通信标准制定方面参与感非常强,也先于竞争对手推出5G智能终端,连同三星/Vivo等也纷纷推出5G终端,而作为智能手机市场绝对的利润 ...
- Tomcat线程池及性能优化(重点)
只需安装Tomcat [root@localhost ~]# vim /usr/local/tomcat8/conf/server.xml 修改处如下: <Connector port=&quo ...
- js中怎么把类数组转化为数组
说起伪数组,首先想到arguments, 这个我们函数参数的一个类数组,是类数组的代表. 1.拥有length属性,可以使用下标来访问元素,这两点和数组相同. 2.不能使用数组的方法,他们不能使用Ar ...
- Mybatis的三种批量操作数据的方法
方法1: 使用for循环在java代码中insert (不推荐) 方法2: 使用 在Mapper.xml当中使用 foreach循环的方式进行insert PersonDao.java文件 publi ...
- Servlet 学习(六)
会话 1.定义 一般意义会话:指两人以上的对话(多用于学习别种语言或方言时) 计算机中的会话:客户端和服务器的通讯 web客户端 A ------>Tomcat web客户端 B ------& ...
- 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:设定引用右对齐
<!DOCTYPE html> <html> <head> <title>菜鸟教程(runoob.com)</title> <meta ...
- tomcat中servlet冲突问题
在启动tomcat以后,控制台发现“Offending class: javax/servlet/Servlet.class”信息: 信息: validateJarFile(E:\code\MyApp ...