Binary Tree Paths
To solve this coding challenge, you need to traverse the binary tree and record all the unique root-to-leaf paths. Each path should be represented using the node values connected by '->'. Such a traversal ensuring to capture all root-to-leaf paths can be done using depth-first search (DFS) due to its natural traversal of every path from the root to the leaves.
Explanation
Given the root of a binary tree, we need to find and return all the paths from the root to each leaf node. A leaf node is defined as a node that has no children (both its left and right child pointers are null). We'll use a depth-first search (DFS) strategy to explore each possible path from the root to the leaves. At each node, we add the node's value to the current path representation. If we reach a leaf node, we'll finalize the path and add it to the list of paths. If the node is not a leaf, we continue the DFS on its children (left and right). Detailed steps:- Initialization - Create a list to store all root-to-leaf paths.
- DFS Implementation :
- Start the DFS from the root node.
- Add the current node's value to the path.
- If the node is a leaf, add the path to the list of paths.
- If not, recursively call DFS on the left and right children.
- Termination - Return the list of paths once DFS completes.
Step-by-Step Explanation/Detailed Steps in Pseudocode
# Define the function which receives the root of the binary tree
Function binaryTreePaths(root):
# Initialize an empty list to store paths
paths = []
# Define inner function to perform depth-first search (DFS)
Function constructPaths(node, path):
# Check if the current node is not null
If node is not null:
# Add the value of the current node to the path
path += node.value.toString()
# Check if the current node is a leaf node
If node.left is null and node.right is null:
# It's a leaf node, add the current path to paths list
paths.add(path)
Else:
# It's not a leaf node, continue exploring its children
# Add separator '->' to the current path
path += '->'
# Recursively call DFS on the left child
constructPaths(node.left, path)
# Recursively call DFS on the right child
constructPaths(node.right, path)
# Initial call to DFS with the root node and an empty path string
constructPaths(root, '')
# Return the list of all root-to-leaf paths found
Return paths
# Example to illustrate the traversal and path capturing
# Input: [1, 2, 3, null, 5]
# Expected Output: ["1->2->5", "1->3"]
# When the function binaryTreePaths is called with the root node of the tree:
# - It starts at the root node (1) with an empty path.
# - Then it moves to the left child (2) and adds '1->' to the path.
# - From node (2), it tries to move to its left child which is null (does nothing).
# - Then it moves to the right child (5) and completes the path '1->2->5'.
# - Since node (5) is a leaf, it adds the path to paths.
# - It backtracks and explores the right child of node (1) which is (3).
# - It completes the path '1->3' and adds it to paths since node (3) is a leaf.
# - Finally, the function returns the list of paths ["1->2->5", "1->3"].
In summary, we use a DFS approach to explore each possible path from the root to the leaves, and at each leaf node, we add the completed path to our list of paths. The process ensures all root-to-leaf paths are captured and returned in the specified format.