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. WordPress <= 4.6 命令执行漏洞(PHPMailer)复现分析

    漏洞信息 WordPress 是一种使用 PHP 语言开发的博客平台,用户可以在支持 PHP 和 MySQL 数据库的服务器上架设属于自己的网站.也可以把 WordPress 当作一个内容管理系统(C ...

  2. OpenCV + python 实现人脸检测(基于照片和视频进行检测)

    OpenCV + python 实现人脸检测(基于照片和视频进行检测) Haar-like 通俗的来讲,就是作为人脸特征即可. Haar特征值反映了图像的灰度变化情况.例如:脸部的一些特征能由矩形特征 ...

  3. js对象与字符串的想到转换

    js JSON.stringify(jsObj);   对象转字符串JSON.parse(str);  字符串转对象

  4. 电子商务(电销)平台中订单模块(Order)数据库设计明细(转)

    以下是自己在电子商务系统设计中的订单模块的数据库设计经验总结,而今发表出来一起分享,如有不当,欢迎跟帖讨论~ 订单表 (order)|-- 自动编号(order_id, 自增长主键)|-- 订单单号( ...

  5. C#WebService服务在Windows7的部署

    (一)C#WebService服务在Windows7的部署  一.在控制面板,程序与卸载中安装IIS组件. 二.IIS无法显示WebService的.asmx文件.解决办法:注:(这里可以不用考虑,直 ...

  6. linux系统下键盘按键的重新映射——xmodmap工具和xev工具

    大家会不会有时候,感觉键盘上的某几个键用起来不是很方便,打字打久了很容易手指头疼呢? 例如大家使用vim编辑器时, 经常需要使用到esc键,而该键在左上角,很不方便的.再比如写程序的时候,经常会使用到 ...

  7. application.properties配置文件

    SpringBoot可以识别两种格式的配置文件,分别是yml文件与properties文件,可以将application.properties文件换成application.yml applicati ...

  8. SQUAD的rnet复现踩坑记

    在港科大rnet(https://github.com/HKUST-KnowComp/R-Net) 实现的基础上做了复现 采用melt框架训练,原因是港科大实现在工程上不是很完美,包括固定了batch ...

  9. 移动端网页 rem css书写

    移动端网页书写   css样式: @charset "utf-8";body,html{font-family: "微软雅黑";font-size:100px; ...

  10. 使用AS-REP Roasting和kerberoasting攻击kerberos

    准备工作 ''' PowerView是PowerSploit框架的一个子集,里面继承了很多和渗透相关的powershell脚本下载地址:https://github.com/PowerShellMaf ...