ICode9

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

LeetCode 1644. Lowest Common Ancestor of a Binary Tree II

2022-04-24 07:31:37  阅读:232  来源: 互联网

标签:Lowest 1644 TreeNode Binary tree node return null root


原题链接在这里:https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree-ii/

题目:

Given the root of a binary tree, return the lowest common ancestor (LCA) of two given nodes, p and q. If either node p or q does not exist in the tree, return null. All values of the nodes in the tree are unique.

According to the definition of LCA on Wikipedia: "The lowest common ancestor of two nodes p and q in a binary 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)". A descendant of a node x is a node y that is on the path from node x to some leaf node.

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. A node can be a descendant of itself according to the definition of LCA.

Example 3:

Input: root = [3,5,1,6,2,0,8,null,null,7,4], p = 5, q = 10
Output: null
Explanation: Node 10 does not exist in the tree, so return null.

Constraints:

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

题解:

The difference between this question and Lowest Common Ancestor of a Binary Tree is that the assumption p and q exist in the tree changes.

Without this assumption, we need to traverse the whole tree and record if we incounter p and q. 

Since we need to treaverse the whole tree, we need to first call the recursive function. Then check if root == p || root == q.

If the condition fulfills, we need to increment the count by 1.

Time Complexity: O(n). n is the number of nodes in the tree.

Space: O(logn).

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     int count = 0;
12     public TreeNode lowestCommonAncestor(TreeNode root, TreeNode p, TreeNode q) {
13         TreeNode res = dfs(root, p, q);
14         return count == 2 ? res : null;
15     }
16     
17     private TreeNode dfs(TreeNode root, TreeNode p, TreeNode q){
18         if(root == null){
19             return null;
20         }
21         
22         TreeNode left = dfs(root.left, p, q);
23         TreeNode right = dfs(root.right, p, q);
24         if(root == p || root == q){
25             count++;
26             return root;
27         }
28         
29         if(left == null){
30             return right;
31         }
32         
33         if(right == null){
34             return left;
35         }
36         
37         return root;
38     }
39 }

类似Lowest Common Ancestor of a Binary Tree.

标签:Lowest,1644,TreeNode,Binary,tree,node,return,null,root
来源: https://www.cnblogs.com/Dylan-Java-NYC/p/16184297.html

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

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

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

ICode9版权所有