
In the given task, you are asked to perform a preorder traversal for a binary tree starting from the node referred to as root. During a preorder traversal, the sequence of operations is: Visiting the root node first, then recursively performing a preorder traversal on the left subtree, followed by the right subtree. The aim is to record and return the values of the nodes in the order they are visited. This form of traversal is particularly useful in scenarios like expression tree evaluations and syntax tree traversal, where the root precedence is critical.
Input:
Output:
Explanation:
Input:
Output:
Explanation:
Input:
Output:
Input:
Output:
[0, 100].-100 <= Node.val <= 100A binary tree preorder traversal involves visiting the nodes in a Root-Left-Right sequence. Here's an intuitive breakdown and a step-by-step explanation for the examples provided:
Example 1:
root = [1, null, 2, 3]1 has no left child and a right child 2.2 has left child 3.1, moves to the right child 2, and then to the left child 3 (Root-Right-Left, because no left child for 1).[1, 2, 3]Example 2:
root = [1, 2, 3, 4, 5, null, 8, null, null, 6, 7, 9]1 is the root, with left child 2 and right child 3.2 has children 4 (left) and 5 (right). 5 has further children 6 (left) and 7 (right).3 has right child 8, and 8 has further left child 9.1, then left child 2, continue to deepest left 4, backtrack and go to 5, then to its children 6 and 7, backtrack to 3 and its subtree 8, then 9.[1, 2, 4, 5, 6, 7, 3, 8, 9]Example 3:
root = [][]Example 4:
root = [1]1.[1]These examples illustrate various scenarios from an empty tree to a more densely populated one, showing the practical application of the Root-Left-Right traversal principle inherent in preorder traversals. The consistency observed in this traversal method lends straightforwardness to tasks such as tree serialization and making copies of the tree.
The provided C++ code implements the Binary Tree Preorder Traversal using an iterative approach with the Morris Traversal technique. Here's how the function traversePreorder operates:
vector<int> named result to store the values of the nodes as they are visited.root node and use a pointer current to traverse the tree.current is not nullptr.current is nullptr:current into result.current to its right child.nullptr, find the rightmost node of the left subtree; this node will temporarily link back to current to avoid usage of stack or recursion.temp->right) is already created:temp->right is nullptr, make temp->right point to current (creating a temporary link), save the current node's value in the result vector, and move current to its left child.temp->right points to current, this means you're revisiting the node and should remove the temporary link and move current to its right child.current becomes nullptr.result vector containing the preorder traversal of the tree.This approach is memory efficient as it does not use additional data structures like stack or recursion for maintaining tree traversal state, and it modifies the tree temporarily during traversal to achieve this.
0 Comments
Be the first to comment and share your perspective with the community.