Similar Problems
Similar Problems not available
Lowest Common Ancestor Of A Binary Tree Iv  Leetcode Solution
Companies:
LeetCode: Lowest Common Ancestor Of A Binary Tree Iv Leetcode Solution
Difficulty: Medium
Topics: hashtable tree binarytree depthfirstsearch
Problem Statement:
You are given the root of a binary tree, along with two nodes, A and B. Write a function to find the lowest common ancestor of the nodes A and B.
Function Signature:
class Solution: def lowestCommonAncestor(self, root: 'TreeNode', nodes: 'List[TreeNode]') > 'TreeNode':
Input:
The input parameters are as follows 
root: A TreeNode representing the root of the binary tree.
nodes: A list of TreeNode objects representing the nodes A and B.
Output:
The function should return a TreeNode object representing the lowest common ancestor of nodes A and B.
Constraints:
1 <= n <= 10^4
Solution Approach:
A binary tree is a tree where each node has at most two children. The left subtree consists of nodes with values less than the parent node, and the right subtree consists of nodes with values greater than the parent node.
For finding the lowest common ancestor of two nodes in a binary tree, we need to traverse the tree from the root node. During this traversal, we need to maintain a list of all the nodes from the root to the node whose lowest common ancestor we are trying to find.
Once we have the node lists for both the given nodes, we can use these lists to find their lowest common ancestor. We traverse both node lists from the root node until we find the last common node. This last common node is the lowest common ancestor of the two given nodes.
Algorithm:
To solve this problem, we will follow the following steps:

Find the node list for both nodes A and B, by starting from the root node and finding each node's path from the root in the binary tree.

Traverse both lists at the same time until we find the last common node. This node will be the lowest common ancestor.

Return the lowest common ancestor found in step 2 as the result.
Code:
The following is the Python3 code implementation of the above approach:
Definition for TreeNode.
class TreeNode(object):
def init(self, x):
self.val = x
self.left = None
self.right = None
class Solution(object): def lowestCommonAncestor(self, root, nodes): """ :type root: TreeNode :type nodes: List[TreeNode] :rtype: TreeNode """ # Convert list to set for O(1) membership testing nodes_set = set(nodes)
def find_path_to_node(node, path):
if node is None:
return False
path.append(node)
# If node is a member of the set, we have found a path to one of the nodes.
if node in nodes_set:
return True
# If the node is not one of the target nodes, recursively check its left and right subtrees.
if (find_path_to_node(node.left, path) or find_path_to_node(node.right, path)):
return True
# If node is neither in the set nor a part of path to one of the nodes.
path.pop()
return False
path_to_a = []
path_to_b = []
# Find paths of nodes A and B from root
find_path_to_node(root, path_to_a)
find_path_to_node(root, path_to_b)
ancestor = None
# Traverse both paths simultaneously until we find the last common node.
for i in range(min(len(path_to_a), len(path_to_b))):
if path_to_a[i] == path_to_b[i]:
ancestor = path_to_a[i]
else:
break
return ancestor
Time Complexity:
The time complexity of this algorithm is O(n), where n represents the total number of nodes in the binary tree.
Space Complexity:
The space complexity of this algorithm is O(n), as we use extra storage to maintain the path lists.
Lowest Common Ancestor Of A Binary Tree Iv Solution Code
1