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.

Note:

  • All of the nodes' values will be unique.
  • p and q are different and both values will exist in the binary tree.

这个题目还是用Divide and Conquer的思想,然后分别在左右两边tree里面找是否有p,或者g,一旦有了,就分别往上面传,只有在left tree, right tree 各有一个点时,就是lowest common ancestor。

Time:O(n),   S: O(n)

Code:

class Solution:
def LCA(self, root, p, g):
if not root or root == p or root == g:
return root
left = self.LCA(root.left, p, g)
right = self.LCA(root.right, p, g)
if left and right:
return root
elif left:
return left
elif right:
return right
return

=> 更简洁

class Solution:
def LCA(self, root, p, g):
if root in [None, p, g]: return root
left, right = self.LCA(root.left, p, g), self.LCA(root.right, p, g)
return root if left and right else left or right

[LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer的更多相关文章

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

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

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

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

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

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

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

  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 Search Tree

    Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...

随机推荐

  1. httpie的使用

    安装 brew install httpie 使用 模拟提交表单 http -f POST yhz.me username=nate 显示详细的请求 http -v yhz.me 只显示Header ...

  2. hdoj:2061

    #include <iostream> #include <string> using namespace std; int main() { int n,k; double ...

  3. win10 Faster-RCNN训练自己数据集遇到的问题集锦 (转)

    题注: 在win10下训练实在是有太多坑了,在此感谢网上的前辈和大神,虽然有的还会把你引向另一个坑~~. 最近,用faster rcnn跑一些自己的数据,数据集为某遥感图像数据集——RSOD,标注格式 ...

  4. AWT是Java最早出现的图形界面,但很快就被Swing所取代。

    Module  11 Swing AWT是Java最早出现的图形界面,但很快就被Swing所取代. Swing才是一种真正的图形开发. AWT在不同平台所出现的界面可能有所不同:因为每个OS都有自己的 ...

  5. Phoenix 5.0 hbase 2.0 org.apache.hadoop.security.authentication.util.KerberosUtil.hasKerberosKeyTab

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  6. IntelliJ IDEA 改变默认的签名 Administrator

    /** * Created with IntelliJ IDEA. * User: Administrator * Date: 12-8-27 * Time: 下午11:29 * To change ...

  7. springboot的工作原理之配置文件的加载

    有一个非常底层的类SpringFactoriesLoader,顾名思义,就是加载工厂的类,没有办法,spring中工厂太多了,加载工厂类也需要一个类,参考博文: https://blog.csdn.n ...

  8. centos7 与 archlinux用户 安装 python3模块 pytaglib

    对于 centos7用户: yum group install "Development Tools" yum install taglib-devel yum install p ...

  9. 25.redux回顾,redux中的action函数异步

    回顾:Redux: 类似于 Vuex 概念:store/reducer/action action:动作 {type,.....} 一定要有type 其他属性不做限制 reducer:通过计算产生st ...

  10. 异常HTTP Status 500 - Illegal access to constructor, is it public? java.lang.IllegalAccessException: Class com.opensymphony.xwork2.ObjectFactory can not access a member of class action.CoreAction with

    Exception report message Illegal access to constructor, is it public? description The server encount ...