diff --git a/1790-lowest-common-ancestor-of-a-binary-tree-iii/README.md b/1790-lowest-common-ancestor-of-a-binary-tree-iii/README.md new file mode 100644 index 0000000..bf7b257 --- /dev/null +++ b/1790-lowest-common-ancestor-of-a-binary-tree-iii/README.md @@ -0,0 +1,49 @@ +

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:

+ + diff --git a/1790-lowest-common-ancestor-of-a-binary-tree-iii/solution.py b/1790-lowest-common-ancestor-of-a-binary-tree-iii/solution.py new file mode 100644 index 0000000..ca2ace2 --- /dev/null +++ b/1790-lowest-common-ancestor-of-a-binary-tree-iii/solution.py @@ -0,0 +1,29 @@ +# Time: O(h), h = height of the tree +# Space: O(h) + +""" +# Definition for a Node. +class Node: + def __init__(self, val): + self.val = val + self.left = None + self.right = None + self.parent = None +""" + +class Solution: + def lowestCommonAncestor(self, p: 'Node', q: 'Node') -> 'Node': + visited = set() + + while q: + visited.add(q.val) + q = q.parent + + while p: + if p.val in visited: + return p + visited.add(p.val) + p = p.parent + + return None +