
Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.
History is littered with hundreds of conflicts over the future of a community, group, location or business that were "resolved" when one of the parties stepped ahead and destroyed what was there. With the original point of contention destroyed, the debates would fall to the wayside. Archive Team believes that by duplicated condemned data, the conversation and debate can continue, as well as the richness and insight gained by keeping the materials. Our projects have ranged in size from a single volunteer downloading the data to a small-but-critical site, to over 100 volunteers stepping forward to acquire terabytes of user-created data to save for future generations.
The main site for Archive Team is at archiveteam.org and contains up to the date information on various projects, manifestos, plans and walkthroughs.
This collection contains the output of many Archive Team projects, both ongoing and completed. Thanks to the generous providing of disk space by the Internet Archive, multi-terabyte datasets can be made available, as well as in use by the Wayback Machine, providing a path back to lost websites and work.
Our collection has grown to the point of having sub-collections for the type of data we acquire. If you are seeking to browse the contents of these collections, the Wayback Machine is the best first stop. Otherwise, you are free to dig into the stacks to see what you may find.
The Archive Team Panic Downloads are full pulldowns of currently extant websites, meant to serve as emergency backups for needed sites that are in danger of closing, or which will be missed dearly if suddenly lost due to hard drive crashes or server failures.
Given a rooted binary tree, return the lowest common ancestor of its deepest leaves.
Recall that:
d
, the depth of each of its children isd+1
.S
of nodes is the nodeA
with the largest depth such that every node in S is in the subtree with rootA
.Example 1:
Example 2:
Example 3:
Constraints:
这道题让我们求一棵二叉树中最深叶结点的最小公共父结点 Lowest Common Ancestor,在 LeetCode 中,有两道关于 LCA 的题,分别是 Lowest Common Ancestor of a Binary Tree 和 Lowest Common Ancestor of a Binary Search Tree,但是显然这道题要更加的复杂一些,因为最深的叶结点的个数不确定,可能会有1个,2个,甚至多个,那么其最小公共父节点的位置也就有多种可能的位置。对于二叉树的问题,刷题老司机们应该都知道,十有八九都是用递归来做,这道题也不例外。在毫无头绪的时候,就先从最简单的情况开始分析吧,假如 root 为空,则直接返回 nullptr,假如 root 没有子结点,其本身就是最深叶结点,返回 root。若 root 有左右子结点,说明左右子树存在,通常情况下我们会对左右子结点调用递归,那么返回的就是左右子树分别的最深叶结点的最小公共父节点,但是问题来了,就算分别知道了左右子树的最深结点的 LCA,怎么推出当前树的 LCA?若左子树的最深叶结点的深度更深,则应该返回左子树的 LCA,若右子树的最深叶结点的深度更深,则应该返回右子树的 LCA,若二者一样深,则要返回当前结点。这样的话,对于每个结点 node,必须要分别知道其左右子树的最深叶结点的深度才行,可以使用一个 getDepth 函数来求任意结点到叶结点的最大深度,叶结点本身的深度为0。有了这个函数,就可以对当前结点的左右子结点计算深度,若深度相同,则返回当前结点,否则对深度大的子结点调用递归,怎么隐约感觉有些二分搜索法的影子在里面,参见代码如下:
解法一:
由于计算深度的函数 getDepth 存在大量的重复计算,可以使用一个 HashMap 来保存已经算过深度的结点,这样再次遇到的时候,直接从 HashMap 中取值即可,可以使计算效率更高一些,参见代码如下:
解法二:
我们也可以把计算 LCA 和深度放到一个子函数中,让子函数 helper 既返回以当前结点为根结点的子树的最深叶结点的 LCA,又返回当前结点的深度。在递归函数 helper 中,首先判空,若为空,则返回由 nullptr 和0组成的 pair 对儿。否则分别对左右子结点调用递归函数,若左结点的深度大,则返回左子结点和左子结点深度加1组成的 pair 对儿;若右子结点的深度大,则返回右子结点和右子结点深度加1组成的 pair 对儿;剩下的情况就是左右子结点的深度相同,返回当前结点和左子结点深度加1组成的 pair 对儿即可,参见代码如下:
解法三:
再来看一种很类似的写法,这里用了两个全局变量,全局最深叶结点的最小公共父节点 res,以及全局的最大深度 deepest。跟上面的解法思路很类似,也是在递归函数 helper 中既算 lCA 又算深度,同时还要更新全局的 res 和 deepest。递归函数还需要一个参数 cur,用来保存当前结点的深度,首先用 cur 来更新最大深度 deepest,再判空,若 node 为空,直接返回 cur。再对左右子结点调用递归函数,假如此时左右子结点返回的深度都等于最大深度 deepest,说明当前结点 node 就是要求的 LCA,赋值给结果 res,然后返回 left 和 right 中的较大值,就是当前结点 node 的深度,参见代码如下:
解法四:
Github 同步地址:
#1123
类似题目:
Lowest Common Ancestor of a Binary Tree
Lowest Common Ancestor of a Binary Search Tree
参考资料:
https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/
https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/discuss/334583/Java-O(n)-Short-and-Simple-Recursion
https://leetcode.com/problems/lowest-common-ancestor-of-deepest-leaves/discuss/334577/JavaC%2B%2BPython-Two-Recursive-Solution
LeetCode All in One 题目讲解汇总(持续更新中...)
The text was updated successfully, but these errors were encountered: