Path Sum Root to Leaf in Binary Tree
📖 Scenario: Imagine you have a family tree where each person has a number representing their age. You want to check if there is a path from the oldest ancestor (root) down to a youngest family member (leaf) where the sum of ages equals a specific number.
🎯 Goal: Build a simple binary tree and write a function to check if there is a root-to-leaf path with a given sum.
📋 What You'll Learn
Create a binary tree node class called
TreeNode with val, left, and right propertiesCreate a binary tree with exact nodes and values as specified
Create a variable called
targetSum with the exact value specifiedWrite a function called
hasPathSum that takes root and targetSum and returns true or falsePrint the result of calling
hasPathSum(root, targetSum)💡 Why This Matters
🌍 Real World
Checking paths in trees is useful in decision-making systems, network routing, and family tree analysis.
💼 Career
Understanding tree traversal and recursion is important for software engineering roles involving data structures and algorithms.
Progress0 / 4 steps