0
0
DSA Goprogramming~20 mins

Lowest Common Ancestor in Binary Tree in DSA Go - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
LCA Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of LCA for given nodes in a binary tree
What is the output of the program when finding the Lowest Common Ancestor (LCA) of nodes 5 and 1 in the given binary tree?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    if root == nil || root == p || root == q {
        return root
    }
    left := lowestCommonAncestor(root.Left, p, q)
    right := lowestCommonAncestor(root.Right, p, q)
    if left != nil && right != nil {
        return root
    }
    if left != nil {
        return left
    }
    return right
}

func main() {
    root := &TreeNode{Val: 3}
    root.Left = &TreeNode{Val: 5}
    root.Right = &TreeNode{Val: 1}
    root.Left.Left = &TreeNode{Val: 6}
    root.Left.Right = &TreeNode{Val: 2}
    root.Right.Left = &TreeNode{Val: 0}
    root.Right.Right = &TreeNode{Val: 8}

    p := root.Left      // Node with Val 5
    q := root.Right     // Node with Val 1

    lca := lowestCommonAncestor(root, p, q)
    fmt.Println(lca.Val)
}
A3
B5
C1
D6
Attempts:
2 left
💡 Hint
Think about the lowest node that has both nodes 5 and 1 as descendants.
Predict Output
intermediate
2:00remaining
Output of LCA for nodes in the same subtree
What is the output of the program when finding the Lowest Common Ancestor (LCA) of nodes 6 and 2 in the given binary tree?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    if root == nil || root == p || root == q {
        return root
    }
    left := lowestCommonAncestor(root.Left, p, q)
    right := lowestCommonAncestor(root.Right, p, q)
    if left != nil && right != nil {
        return root
    }
    if left != nil {
        return left
    }
    return right
}

func main() {
    root := &TreeNode{Val: 3}
    root.Left = &TreeNode{Val: 5}
    root.Right = &TreeNode{Val: 1}
    root.Left.Left = &TreeNode{Val: 6}
    root.Left.Right = &TreeNode{Val: 2}
    root.Right.Left = &TreeNode{Val: 0}
    root.Right.Right = &TreeNode{Val: 8}

    p := root.Left.Left   // Node with Val 6
    q := root.Left.Right  // Node with Val 2

    lca := lowestCommonAncestor(root, p, q)
    fmt.Println(lca.Val)
}
A2
B6
C5
D3
Attempts:
2 left
💡 Hint
Both nodes are in the left subtree of the root.
🔧 Debug
advanced
2:00remaining
Identify the error in LCA function implementation
What error will occur when running this LCA function code?
DSA Go
func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    if root == nil {
        return nil
    }
    if root == p || root == q {
        return root
    }
    left := lowestCommonAncestor(root.Left, p, q)
    right := lowestCommonAncestor(root.Right, p, q)
    if left != nil && right != nil {
        return root
    }
    if left != nil {
        return left
    }
    return right
}
APanic due to nil pointer dereference if p or q is nil
BNo error, works correctly
CInfinite recursion causing stack overflow
DCompilation error due to missing return statement
Attempts:
2 left
💡 Hint
Check how the function handles nil and base cases.
Predict Output
advanced
2:00remaining
Output when one node is ancestor of the other
What is the output of the program when finding the Lowest Common Ancestor (LCA) of nodes 5 and 6 in the given binary tree?
DSA Go
package main

import "fmt"

type TreeNode struct {
    Val   int
    Left  *TreeNode
    Right *TreeNode
}

func lowestCommonAncestor(root, p, q *TreeNode) *TreeNode {
    if root == nil || root == p || root == q {
        return root
    }
    left := lowestCommonAncestor(root.Left, p, q)
    right := lowestCommonAncestor(root.Right, p, q)
    if left != nil && right != nil {
        return root
    }
    if left != nil {
        return left
    }
    return right
}

func main() {
    root := &TreeNode{Val: 3}
    root.Left = &TreeNode{Val: 5}
    root.Right = &TreeNode{Val: 1}
    root.Left.Left = &TreeNode{Val: 6}
    root.Left.Right = &TreeNode{Val: 2}
    root.Right.Left = &TreeNode{Val: 0}
    root.Right.Right = &TreeNode{Val: 8}

    p := root.Left      // Node with Val 5
    q := root.Left.Left // Node with Val 6

    lca := lowestCommonAncestor(root, p, q)
    fmt.Println(lca.Val)
}
A6
B3
C2
D5
Attempts:
2 left
💡 Hint
If one node is ancestor of the other, the ancestor is the LCA.
🧠 Conceptual
expert
2:00remaining
Understanding LCA algorithm behavior with missing nodes
If one of the nodes (p or q) is not present in the binary tree, what will the LCA function return?
AReturns the node that is present if found, otherwise nil
BReturns nil because both nodes must be present to find LCA
CReturns the root node always regardless of presence
DCauses a runtime error due to nil pointer dereference
Attempts:
2 left
💡 Hint
Consider how the function returns when it finds p or q and when it doesn't.