Root to Node Path in Binary Tree

Hard Problems DSA practice problem on Onlearn.

Difficulty: medium.

Topics: Finding the Path from Root to a Given Leaf Node in a Binary Tree, Binary Tree, Depth-First Search, Recursion, Backtracking, Tree Traversal, Time Complexity, Space Complexity, backtracking, dfs, binary tree, recursion, pathfinding, tree traversal, Path to Node.

Path from Root to a Given Leaf Node in a Binary Tree Problem Statement Given the root of a binary tree and an integer targetNodeValue representing the value of a leaf node in the tree, return the path from the root to the node with value targetNodeValue. It is guaranteed that: No two nodes in the tree have the same data value. The targetNodeValue is present in the tree. A path to the targetNodeValue always exists, and targetNodeValue is a leaf node. Input Specification The first line contains a space separated sequence of integers representing the binary tree in level order traversal. A value of 1 indicates a null child. The second line contains a single integer, targetNodeValue, representing the value of the target leaf node. Output Specification Return a list of integers representing the values of the nodes in the path from the root to the targetNodeValue (inclusive). Constraints The tree will have at least one node. Node values will be unique. The target node is guaranteed to be a leaf node. Sample Test Cases Sample Input 1: Sample Output 1: Explanation 1: The tree structure is: The path from root (1) to leaf (7) is 1 2 5 7. Sample Input 2: Sample Output 2: Explanation 2: The tree structure is: The path from root (1) to leaf (6) is 1 3 5 6.