Tree traversal and return vector list.

//In case your pre, in, post-order traversal function wants you to return the list, not to print them.
create another function lst1, and pass vector with refernce.

void lst1(struct Node*root, vector<int> &v)
{
    if (root==0)
    return;
    else
    v.push_back(root->data);
    lst1(root->left,v);
    lst1(root->right,v);
}
vector <int> preorder(Node* root)
{
  vector<int>v;
  lst1(root,v);
  return v;
}

Comments

Popular posts from this blog

First_Come_First_Serve CPU Scheduling

Reversing stack Method 2 !! (One Helper Stack only)

Populating Next Right Pointers in Each Node in O(1) space (without queue and level order)

Calculate factorial of large numbers !! (Using Arrays)

Multiplication of large numbers (Given in string format)

Left View of Binary Tree (Method 1 using recursion)

Check Bracket Sequence

Image Multiplication

Boundary Traversal of binary tree

BST to greater sum tree