[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 ...
随机推荐
- php cli模式下获取参数的方法
转载声明:http://blog.csdn.net/fdipzone/article/details/51945892 php在cli模式下接收参数有两种方法 1.使用argv数组 <?php ...
- Android Studio编译报错“java.lang.OutOfMemoryError: GC overhead limit exceeded
1.在build.gradle添加脚本指定编译堆内存 如果在整个工程中生效,则在build.gradle中增加如下配置: android { .............. dexOptions { i ...
- MVC教程四:Controller向View传值的几种方式
一.通过ViewData传值 MVC从开始版本就一直支持使用ViewData将Controller里面的数据传递到View.ViewData定义如下: 从上面的截图中可以看出,ViewData里面存的 ...
- class ObjectOutputStream也是过滤流,使节点流直接获得输出对象。
class ObjectOutputStream也是过滤流,使节点流直接获得输出对象. 最有用的方法:WriteObject(Object b) 用流传输对象称为对象的序列化,但并不使所有的对象都可以 ...
- return在try...except...finally...中的表现
一直以为return就直接退出函数了,最近遇到一情况,在try中return,并没有什么卵用,还是会去执行finally里的内容,导致不能正确返回想要的数据 一直以为return就会跳出函数,发现原来 ...
- JS中toString()、toLocaleString()、valueOf()的区别
前言 Array.Boolean.Date.Number等对象都具有 toString().toLocaleString().valueOf()三个方法,那这三个方法有什么区别? 一.JS Array ...
- LeetCode - Subarray sum equals k
Given an array of integers and an integer k, you need to find the total number of continuous subarra ...
- 怎么下载geventwebsocket
pip install gevent-websocket sudo pip install gevent-websocket
- window下强制删除文件
在windows系统下,有时候总有一下没办法删除的文件,这个时候可以使用一个命令行脚本强行删除 步骤如下: 把以下代码保存为一个delete.txt文件 DEL /F /A /Q \\?\%1 RD ...
- Eclipse的设置、调优、使用(解决启动卡顿等问题)----转
eclipse调优 一般在不对eclipse进行相关设置的时候,使用eclipse总是会觉得启动好慢,用起来好卡,其实只要对eclipse的相关参数进行一些配置,就会有很大的改善. 加快启动速度 1. ...