Daily Dose of DSA - Day 22
B.T Preorder Traversal using Recursion (TC:O(n) & SC:O(n)(call stack)) & Stack with Iteration (TC:O(n) & SC:O(n))
Table of contents
Question Link: https://leetcode.com/problems/binary-tree-preorder-traversal/description/
Using Recursion :
The
preorder
function is a recursive helper function that takes aTreeNode*
parameter representing the current node during traversal.If the current node is
NULL
, the function returns, as there are no nodes to process.If the current node is not
NULL
, its value (root->val
) is added to theans
vector, which stores the pre-order traversal result.The function then recursively calls itself with the left child of the current node (
root->left
) to traverse the left subtree.After the left subtree traversal, the function recursively calls itself with the right child of the current node (
root->right
) to traverse the right subtree.The recursive calls continue until all nodes in the binary tree have been traversed.
The
preorderTraversal
function is the entry point of the code. It takes aTreeNode*
parameter representing the root of the binary tree.Inside the
preorderTraversal
function, thepreorder
function is called to perform the pre-order traversal starting from the root node.Finally, the
ans
vector, which stores the pre-order traversal result, is returned.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int>ans;
void preorder(TreeNode* root)
{
if(root==NULL) return;
ans.push_back(root->val);
preorder(root->left);
preorder(root->right);
}
vector<int> preorderTraversal(TreeNode* root) {
preorder(root);
return ans;
}
};
Using Stack with Iteration:
The
preorderTraversal
function takes aTreeNode*
parameterroot
, representing the root of the binary tree.Inside the function, an empty vector
ans
is initialized to store the pre-order traversal result.If the
root
isNULL
, indicating an empty tree, the function directly returns the emptyans
vector.If the
root
is notNULL
, a stack ofTreeNode*
namedst
is created to perform iterative pre-order traversal. The stack is initially populated with theroot
node.While the stack is not empty, the function enters a loop to process the nodes.
In each iteration of the loop, the top node
x
is popped from the stack usingst.top
()
and removed usingst.pop()
.The value of the popped node
x->val
is added to theans
vector, capturing the pre-order traversal sequence.If the popped node
x
has a right child (x->right
is notNULL
), it is pushed onto the stack to be processed later.If the popped node
x
has a left child (x->left
is notNULL
), it is also pushed onto the stack to be processed later.The loop continues until all nodes in the binary tree have been processed.
Finally, the function returns the
ans
vector containing the pre-order traversal result.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
vector<int> ans;
if(root==NULL) return ans;
stack<TreeNode*>st;
st.push(root);
while(!st.empty())
{
TreeNode* x = st.top();
ans.push_back(x->val);
st.pop();
if(x->right!=NULL) st.push(x->right);
if(x->left!=NULL) st.push(x->left);
}
return ans;
}
};