LeetCode 1123. Lowest Common Ancestor of Deepest Leaves?

经典LCA的变形题目
现在不是要求 你找任意给定的两个Node的LCA了 而是找最深leaves的LCA。
回想一下 之前解决这个问题的方法是:通过preorder traverse 来构建一个child-parent的hashmap,然后再从p出发得到所有可能的的LCA 然后从q出发继续检索。找到第一个再LCA pool里面的 那就是真正的LCA。

但是我们如何解决这个问题呢?
注意:所有节点的值都是Unique的。

但是我根本就没明白这个题目到底想干嘛, 如果有多组deepest leaves那么应该怎么办呢?
据题目说这道题目和LC865一样,看一下LC865的题干
Given the root of a binary tree, the depth of each node is the shortest distance to the root.
Return the smallest subtree such that it contains all the deepest nodes in the original tree.
A node is called the deepest if it has the largest depth possible among any node in the entire tree.
The subtree of a node is tree consisting of that node, plus the set of all descendants of that node.

我不认为我会在面试中遇到这个题目,如果遇到,装作认真研究的样子就行了。

posted @ 2020-11-18 10:37  EvanMeetTheWorld  阅读(18)  评论(0)    收藏  举报