Lowest Common Ancestor in Binary Tree
📖 Scenario: You are working on a family tree application. You want to find the closest common ancestor of two family members in the tree.
🎯 Goal: Build a TypeScript program to find the Lowest Common Ancestor (LCA) of two nodes in a binary tree.
📋 What You'll Learn
Create a binary tree using a
TreeNode class with val, left, and right properties.Create a binary tree with the exact structure given.
Write a function
lowestCommonAncestor that takes the root node and two nodes p and q and returns their lowest common ancestor node.Print the value of the lowest common ancestor node.
💡 Why This Matters
🌍 Real World
Finding the lowest common ancestor is useful in family trees, organizational charts, and network routing to find shared connections.
💼 Career
Understanding tree traversal and recursion is important for software engineering roles involving data structures, algorithms, and system design.
Progress0 / 4 steps