Given two nodes of a binary tree p and q, return their lowest common ancestor (LCA).
Each node will have a reference to its parent node. The definition for Node is below:
class Node {
public int val;
public Node left;
public Node right;
public Node parent;
}
According to the definition of LCA on Wikipedia: "The lowest common ancestor of two nodes p and q in a tree T is the lowest node that has both p and q as descendants (where we allow a node to be a descendant of itself)."
Example 1:

Input: root = [3,

本篇博客主要介绍了LeetCode 1650题目的解决方法,即在二叉树中找到两个给定节点的最低公共祖先(LCA)。题目中每个节点包含指向父节点的引用。通过分析节点路径,可以发现问题与链表相交类似。提出了两种解决方案:一是使用集合记录一个节点的父节点路径,检查另一个节点的父节点是否在集合中;二是利用双指针技巧,沿着两个节点的父节点路径同步移动,找到公共祖先节点。
最低0.47元/天 解锁文章
256

被折叠的 条评论
为什么被折叠?



