Daily Dose of DSA - Day 23
B.T inorder traversal using Recursion (TC:O(n) & SC:O(n)(call stack)) & Stack with Iteration (TC:O(n) & SC:O(n))
Table of contents
https://leetcode.com/problems/binary-tree-inorder-traversal/description/
Using Recursion :
The
inorder
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
, the function recursively calls itself with the left child of the current node (root->left
) to traverse the left subtree.After the left subtree traversal, the value of the current node (
root->val
) is added to theans
vector, which stores the in-order traversal result.Finally, 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
inorderTraversal
function is the entry point of the code. It takes aTreeNode*
parameter representing the root of the binary tree.Inside the
inorderTraversal
function, theinorder
function is called to perform the in-order traversal starting from the root node.Finally, the
ans
vector, which stores the in-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 inorder(TreeNode*root)
{
if(root==NULL) return;
inorder(root->left);
ans.push_back(root->val);
inorder(root->right);
}
vector<int> inorderTraversal(TreeNode* root) {
inorder(root);
return ans;
}
};
Using Stack with Iteration :
The
inorderTraversal
function takes aTreeNode*
parameterroot
, representing the root of the binary tree.Inside the function, a stack of
TreeNode*
namedst
is created to perform iterative in-order traversal.A
TreeNode*
variablenode
is initialized with the value ofroot
, indicating the current node during traversal.An empty vector
ans
is initialized to store the in-order traversal result.The code enters an infinite loop that breaks when both the stack is empty and
node
isNULL
.Inside the loop, if
node
is notNULL
, it means there are more left nodes to traverse. In this case, the current node is pushed onto the stack usingst.push(node)
andnode
is updated to its left childnode->left
.If
node
isNULL
, it means there are no more left nodes to traverse. In this case, the code checks if the stack is empty. If it is, it breaks the loop as all nodes have been processed.If the stack is not empty, the top node
node
is popped from the stack usingst.top
()
and removed usingst.pop()
. Its valuenode->val
is added to theans
vector, capturing the in-order traversal sequence.After adding the current node to the
ans
vector,node
is updated to its right childnode->right
.The loop continues until all nodes in the binary tree have been processed.
Finally, the function returns the
ans
vector containing the in-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> inorderTraversal(TreeNode* root) {
stack<TreeNode*>st;
TreeNode* node = root;
vector<int> ans;
while(true)
{
if(node!=NULL)
{
st.push(node);
node = node->left;
}
else
{
if(st.empty()) break;
node = st.top();
st.pop();
ans.push_back(node->val);
node = node->right;
}
}
return ans;
}
};