"""
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的更多相关文章

  1. 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 利用二 ...

  2. 【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 ...

  3. 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 ...

  4. 88 Lowest Common Ancestor of a Binary Tree

    原题网址:https://www.lintcode.com/problem/lowest-common-ancestor-of-a-binary-tree/description 描述 给定一棵二叉树 ...

  5. 【刷题-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 ...

  6. [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 ...

  7. LeetCode Lowest Common Ancestor of a Binary Tree

    原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ 题目: Given a binary tr ...

  8. [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 ...

  9. [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 ...

随机推荐

  1. 【剑指Offer面试编程题】题目1509:树中两个结点的最低公共祖先--九度OJ

    题目描述: 给定一棵树,同时给出树中的两个结点,求它们的最低公共祖先. 输入: 输入可能包含多个测试样例. 对于每个测试案例,输入的第一行为一个数n(0<n<1000),代表测试样例的个数 ...

  2. 「SDOI2009」Bill的挑战

    「SDOI2009」Bill的挑战 传送门 状压 \(\text{DP}\) 瞄一眼数据范围 \(N\le15\),考虑状压. 设 \(f[i][j]\) 表示在所有串中匹配到第 \(i\) 位字符且 ...

  3. Matlab利用subplot绘制多个图像

    利用subplot绘制多个图像 subplot(m,n,p) subplot是将多个图画到一个平面上的函数,m是行,n是列,p是所要绘制图所在的位置 x = 0:0.1:100; sinY = sin ...

  4. SpringBoot中普通类无法通过@Autowired自动注入Service、dao等bean解决方法

    无法注入原因: 有的时候我们有一些类并不想注入Spring容器中,有Spring容器实例化,但是我们又想使用Spring容器中的一些对象,所以就只能借助工具类来获取了 工具类: package com ...

  5. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:引用(Blockquote)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 吴裕雄 Bootstrap 前端框架开发——Bootstrap 排版:内联子标题

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  7. static在c\c++中的作用(翁恺c++公开课[28-29]学习笔记)

    static相对来说是一个较复杂的修饰符,c++中的static在c的基础之上又包含了static在类中的应用(也就是说多了static的成员变量和static的成员函数):c\c++中静态变量.对象 ...

  8. Android开发实例之miniTwitter登录界面的实现

    原文: http://www.jizhuomi.com/android/example/134.html 本文要演示的Android开发实例是如何完成一个Android中的miniTwitter登录界 ...

  9. git rebase 与git merge 小结

    git merge是用来合并两个分支的. $ git merge b   将b分支合并到当前分支 同样  $ git rebase b ,也是把 b分支合并到当前分支 ---------------- ...

  10. freemarker作为PDF模板实现下载功能

    freemarker 文件 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type ...