ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

[LeetCode] 1650. Lowest Common Ancestor of a Binary Tree III

2022-07-08 07:31:42  阅读:144  来源: 互联网

标签:Lowest Binary node parent int Tree 节点 public Node


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 = 5, q = 1
Output: 3
Explanation: The LCA of nodes 5 and 1 is 3.

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 nodes 5 and 4 is 5 since a node can be a descendant of itself according to the LCA definition.

Example 3:

Input: root = [1,2], p = 1, q = 2
Output: 1

Constraints:

  • The number of nodes in the tree is in the range [2, 105].
  • -109 <= Node.val <= 109
  • All Node.val are unique.
  • p != q
  • p and q exist in the tree.

二叉树的最近公共祖先 III。

给定一棵二叉树中的两个节点 p 和 q,返回它们的最近公共祖先节点(LCA)。

每个节点都包含其父节点的引用(指针)。Node 的定义如下:

class Node {
public int val;
public Node left;
public Node right;
public Node parent;
}
根据维基百科中对最近公共祖先节点的定义:“两个节点 p 和 q 在二叉树 T 中的最近公共祖先节点是后代节点中既包括 p 又包括 q 的最深节点(我们允许一个节点为自身的一个后代节点)”。一个节点 x 的后代节点是节点 x 到某一叶节点间的路径中的节点 y。

来源:力扣(LeetCode)
链接:https://leetcode.cn/problems/lowest-common-ancestor-of-a-binary-tree-iii
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

这道题跟前两个版本的区别是多了一个 parent 节点。这样我们就可以从当前节点反过来往回找父节点是谁。既然还是找两个节点的最小公共父节点,那么我们就从两个节点分别开始找他们各自的父节点。这里我首先去看一下两个节点的深度分别是多少,并把他们的深度先调整成一样。当深度一样的时候,方便两个节点同时往他们各自的父节点走,这样他们可以同时到达他们共同的父节点。

时间O(h)

空间O(1)

Java实现

 1 /*
 2 // Definition for a Node.
 3 class Node {
 4     public int val;
 5     public Node left;
 6     public Node right;
 7     public Node parent;
 8 };
 9 */
10 
11 class Solution {
12     public Node lowestCommonAncestor(Node p, Node q) {
13         int pDepth = getDepth(p);
14         int qDepth = getDepth(q);
15         while (pDepth > qDepth) {
16             pDepth--;
17             p = p.parent;
18         }
19         while (pDepth < qDepth) {
20             qDepth--;
21             q = q.parent;
22         }
23 
24         while (p != q) {
25             p = p.parent;
26             q = q.parent;
27         }
28         return p;
29     }
30 
31     private int getDepth(Node node) {
32         int depth = 0;
33         while (node != null) {
34             node = node.parent;
35             depth++;
36         }
37         return depth;
38     }
39 }

 

相关题目

235. Lowest Common Ancestor of a Binary Search Tree

236. Lowest Common Ancestor of a Binary Tree

865. Smallest Subtree with all the Deepest Nodes

1257. Smallest Common Region

1650. Lowest Common Ancestor of a Binary Tree III

LeetCode 题目总结

标签:Lowest,Binary,node,parent,int,Tree,节点,public,Node
来源: https://www.cnblogs.com/cnoodle/p/16456888.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有