




Sponsored
Sponsored
This approach uses recursion to traverse the tree starting from the root. If the current node is either p or q, then the node is returned upwards in the recursion stack as the potential LCA. Otherwise, we continue to search both left and right subtrees. If both subtrees return non-null values, it means p and q are in different subtrees, and the current node is the LCA. If only one subtree returns a non-null value, it means both nodes are located in that subtree and that subtree's root should be the LCA.
Time Complexity: O(N), where N is the number of nodes in the binary tree, as we visit each node only once. 
Space Complexity: O(N) due to the recursion stack when the tree is completely unbalanced.
1function TreeNode(val) {
2    this.val = val;
3    this.left = this.right = null;
4}
The JavaScript code implements a straightforward recursion strategy similar to other languages. It recursively checks the presence of p or q and uses the results from left and right recursive calls to determine the potential LCA.
The basic idea is to find the paths from the root to the two nodes p and q. Once you have the paths, compare them to find the deepest common node. This method is straightforward, using known operations to reconfirm ancestor status along determined paths.
Time Complexity: O(N) due to double traversal in finding paths for each node. 
Space Complexity: O(H), where H is the height of the tree for storing path information.
This Python implementation uses two path lists to track node descent from root to the two target nodes, returning the last common node.