0
0
DSA Goprogramming~30 mins

Path Sum Root to Leaf in Binary Tree in DSA Go - Build from Scratch

Choose your learning style9 modes available
Path Sum Root to Leaf in Binary Tree
📖 Scenario: Imagine you have a simple family tree where each person has a number representing their age. You want to find out 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: You will build a small binary tree with exact ages, set a target sum, write a function to check if any root-to-leaf path sums to the target, and print the result.
📋 What You'll Learn
Create a binary tree with nodes having exact integer values
Set a target sum variable
Write a function called hasPathSum that takes the root node and target sum and returns a boolean
Print the boolean result of hasPathSum
💡 Why This Matters
🌍 Real World
Checking path sums in trees is useful in decision trees, network routing, and financial calculations where paths represent sequences of choices or transactions.
💼 Career
Understanding tree traversal and recursion is fundamental for software engineers working with hierarchical data, algorithms, and system design.
Progress0 / 4 steps
1
Create the binary tree structure and nodes
Create a struct called TreeNode with fields Val int, Left *TreeNode, and Right *TreeNode. Then create a variable called root representing the root node with value 5. Set its left child to a node with value 4 and its right child to a node with value 8. The left child of the node with value 4 should be a node with value 11. The left and right children of the node with value 8 should be nodes with values 13 and 4 respectively. Finally, the node with value 11 should have left and right children with values 7 and 2. The right child of the node with value 4 (right subtree) should have a right child with value 1.
DSA Go
Hint

Start by defining the TreeNode struct. Then create the root node and assign its children exactly as described.

2
Set the target sum variable
Add a variable called targetSum and set it to 22.
DSA Go
Hint

Just add a new variable targetSum inside the main function and assign it the value 22.

3
Write the hasPathSum function
Write a function called hasPathSum that takes parameters root *TreeNode and sum int and returns a bool. The function should return true if there exists a root-to-leaf path in the tree where the sum of node values equals sum. Otherwise, return false. Use recursion to check the left and right subtrees, subtracting the current node's value from sum. If the node is a leaf (no left and right children), check if the remaining sum equals the node's value.
DSA Go
Hint

Use recursion. Check if the node is nil, then if it's a leaf, compare its value to the sum. Otherwise, recursively check left and right children with the updated sum.

4
Print the result of hasPathSum
Add a print statement in the main function to display the result of calling hasPathSum(root, targetSum).
DSA Go
Hint

Use println(hasPathSum(root, targetSum)) inside the main function to show the result.