[LeetCode] 236. Lowest Common Ancestor of a Binary Tree_ Medium tag: DFS, Divide and conquer
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 nodes5and1is3.
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 nodes5and4is5, 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的更多相关文章
- 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 ...
- [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 ...
- 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 ...
- 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 ...
- (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 ...
- [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 ...
- [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 Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...
随机推荐
- ffmpeg安装步骤
首先要安装各种解码器 1.lame lame-3.99.5.tar.gz Url:http://sourceforge.net/project/showfiles.php?group_id=290&a ...
- 通过T-SQL语句实现数据库加解密功能
CREATE TABLE [dbo].[Users] ( [U_nbr] NVARCHAR(20) NOT NULL PRIMARY KEY, [Pwd] nvarchar(MAX) ) --加密 D ...
- vue-cli关闭eslint及配置eslint
有了eslint的校验,可以来规范开发人员的代码,是挺好的.但是有些像缩进.空格.空白行之类的规范,在开发过程中一直报错,有点烦人了. 我们可以在创建工程的时候选择不要安装eslint.就是在安装工程 ...
- Android Bluetooth hci 命令分析
Android在连接BLE设备的时候,遇到连接没多久就自动断开的情况.通过HCI来分析一下. BLE设备发送连接参数更新请求 3909 15:53:01.224737 TexasIns_f0:d3:4 ...
- 使用git命令push到自己的仓库,显示Unknown且没有贡献记录的解决方案
一.问题的起因 今天用公司电脑在github上push时出现了以下问题: 用户名为unknown: 贡献记录为0: 二.解决方案 1,检查一遍自己的账号密码是否正确,如果正确,执行第二步骤操作: 2, ...
- 加入ffmpeg播放视屏
下面的字反了..,另外没声音 2018-4-28 前段时间已经做的差不多了,音频的pack取出来用openAL播放,并实现了视屏同步播放,并且支持unity 现在的问题就是支持大分辨率视屏播放的问题, ...
- Java8学习笔记目录
Java8学习笔记(一)--Lambda表达式 Java8学习笔记(二)--三个预定义函数接口 Java8学习笔记(三)--方法引入 Java8学习笔记(四)--接口增强 Java8学习笔记(五)-- ...
- linux 测试网络url命令
1.curl 地址 或者 wget url地址 [hxxxx@iZ23vy2msooZ ~]$ curl www.baidu.com<!DOCTYPE html><!--STATUS ...
- maven jdk 版本配置
一种是配置 pom.xml,一种是配置 settings.xml. 方式一:settings.xml 配置 打开 %maven%/conf/settings.xml 文件并编辑它(%maven% 表示 ...
- 写在开始前---ajax中的会话过期与重新登录
一般情况下,点击<a>链接或浏览器输入url时,请求到后端,服务器判断会话是否过期.过期,重定向到登录页,或返回登录页的页面.在ajax中,返回重定向无效,这个时候就需要自己在ajax的逻 ...