0
0
DSA Goprogramming~20 mins

Path Sum Root to Leaf in Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Path Sum Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Path Sum Check for a Simple Tree
What is the output of the following Go code that checks if a path sum exists from root to leaf?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val int
    Left *TreeNode
    Right *TreeNode
}
func hasPathSum(root *TreeNode, sum int) bool {
    if root == nil {
        return false
    }
    if root.Left == nil && root.Right == nil {
        return root.Val == sum
    }
    return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)
}
func main() {
    root := &TreeNode{Val: 5}
    root.Left = &TreeNode{Val: 4}
    root.Right = &TreeNode{Val: 8}
    root.Left.Left = &TreeNode{Val: 11}
    root.Left.Left.Left = &TreeNode{Val: 7}
    root.Left.Left.Right = &TreeNode{Val: 2}
    root.Right.Left = &TreeNode{Val: 13}
    root.Right.Right = &TreeNode{Val: 4}
    root.Right.Right.Right = &TreeNode{Val: 1}
    fmt.Println(hasPathSum(root, 22))
}
ACompilation error
Bfalse
Ctrue
DRuntime panic
Attempts:
2 left
💡 Hint
Trace the path sums from root to leaf nodes and check if any equals 22.
Predict Output
intermediate
2:00remaining
Output of Path Sum Check with No Valid Path
What does the following Go code print when checking for a path sum of 26?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val int
    Left *TreeNode
    Right *TreeNode
}
func hasPathSum(root *TreeNode, sum int) bool {
    if root == nil {
        return false
    }
    if root.Left == nil && root.Right == nil {
        return root.Val == sum
    }
    return hasPathSum(root.Left, sum-root.Val) || hasPathSum(root.Right, sum-root.Val)
}
func main() {
    root := &TreeNode{Val: 1}
    root.Left = &TreeNode{Val: 2}
    root.Right = &TreeNode{Val: 3}
    fmt.Println(hasPathSum(root, 26))
}
Afalse
BCompilation error
Ctrue
DRuntime panic
Attempts:
2 left
💡 Hint
Check if any root-to-leaf path sums to 26 in this small tree.
🔧 Debug
advanced
2:00remaining
Identify the Error in Path Sum Function
What error does the following Go code produce when run?
DSA Go
package main
import "fmt"
type TreeNode struct {
    Val int
    Left *TreeNode
    Right *TreeNode
}
func hasPathSum(root *TreeNode, sum int) bool {
    if root == nil {
        return false
    }
    if root.Left == nil && root.Right == nil {
        return root.Val == sum
    }
    return hasPathSum(root.Left, sum-root.Val) && hasPathSum(root.Right, sum-root.Val)
}
func main() {
    root := &TreeNode{Val: 5}
    root.Left = &TreeNode{Val: 4}
    root.Right = &TreeNode{Val: 8}
    fmt.Println(hasPathSum(root, 9))
}
ARuntime panic
Bfalse
CCompilation error
Dtrue
Attempts:
2 left
💡 Hint
Check the logical operator used to combine recursive calls.
🧠 Conceptual
advanced
2:00remaining
Number of Root-to-Leaf Paths with Given Sum
Given a binary tree, which approach correctly counts how many root-to-leaf paths sum to a target value?
AUse recursion to explore all paths, increment count when leaf node sum matches target, and sum counts from left and right subtrees.
BUse breadth-first search and stop at first path that matches sum to count paths.
CReturn true if any path sum matches target, else false; count is number of true returns.
DOnly check sums of left subtree paths and multiply by right subtree sums to get count.
Attempts:
2 left
💡 Hint
Think about exploring all paths and accumulating counts.
🚀 Application
expert
3:00remaining
Find All Root-to-Leaf Paths with Given Sum
Which code snippet correctly returns all root-to-leaf paths where the sum of node values equals the target sum?
A
func pathSum(root *TreeNode, sum int) [][]int {
    var res [][]int
    var path []int
    var dfs func(*TreeNode, int)
    dfs = func(node *TreeNode, currSum int) {
        if node == nil {
            return
        }
        path = append(path, node.Val)
        currSum += node.Val
        if node.Left == nil && node.Right == nil && currSum == sum {
            temp := make([]int, len(path))
            copy(temp, path)
            res = append(res, temp)
        } else {
            dfs(node.Left, currSum)
            dfs(node.Right, currSum)
        }
        path = path[:len(path)-1]
    }
    dfs(root, 0)
    return res
}
B
func pathSum(root *TreeNode, sum int) [][]int {
    var res [][]int
    var dfs func(*TreeNode, int, []int)
    dfs = func(node *TreeNode, currSum int, path []int) {
        if node == nil {
            return
        }
        currSum += node.Val
        path = append(path, node.Val)
        if node.Left == nil && node.Right == nil && currSum == sum {
            res = append(res, path)
        }
        dfs(node.Left, currSum, path)
        dfs(node.Right, currSum, path)
    }
    dfs(root, 0, []int{})
    return res
}
C
func pathSum(root *TreeNode, sum int) [][]int {
    var res [][]int
    var path []int
    var dfs func(*TreeNode, int)
    dfs = func(node *TreeNode, currSum int) {
        if node == nil {
            return
        }
        path = append(path, node.Val)
        currSum += node.Val
        if node.Left == nil && node.Right == nil && currSum == sum {
            res = append(res, path)
        } else {
            dfs(node.Left, currSum)
            dfs(node.Right, currSum)
        }
        path = path[:len(path)-1]
    }
    dfs(root, 0)
    return res
}
D
func pathSum(root *TreeNode, sum int) [][]int {
    var res [][]int
    var dfs func(*TreeNode, int, []int)
    dfs = func(node *TreeNode, currSum int, path []int) {
        if node == nil {
            return
        }
        currSum += node.Val
        path = append(path, node.Val)
        if node.Left == nil && node.Right == nil && currSum == sum {
            temp := make([]int, len(path))
            copy(temp, path)
            res = append(res, temp)
        }
        dfs(node.Left, currSum, path)
        dfs(node.Right, currSum, path)
    }
    dfs(root, 0, []int{})
    return res
}
Attempts:
2 left
💡 Hint
Look for correct path copying to avoid shared slices in results.