LeetCode 1650. Lowest Common Ancestor of a Binary Tree III - 二叉树(Binary Tree)系列题3

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

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,5,1,6,2,0,8,null,null,7,4], p = 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值