Problem 47

Find In Tree Recursion Challenge

Given a tree node and a target value, determine whether the target exists anywhere in the tree. Each node is an object with a value and a children array.

Function Signature

treeIncludes(node, target)

Parameters

  • node — a tree node with the structure {value: any, children: [node, ...]}.
  • target — the value to search for.

Data Structure

{
  value: any,
  children: Array  // each element is a node with the same shape
}

Output

Return true if the target is found in any node's value, or false otherwise.

Constraints

  • The function must use recursion.
  • The input tree must not be mutated.
  • The function should accept exactly two arguments.

Examples

treeIncludes({value: 'root', children: [{value: 'leaf', children: []}]}, 'leaf')
→ true

treeIncludes({value: 1, children: [{value: 2, children: [{value: 3, children: []}]}]}, 4)
→ false

treeIncludes({value: 'a', children: []}, 'a')
→ true