Problem Description
Given a binary tree, return the preorder traversal of its nodes values..
Problem Constraints
1 <= number of nodes <= 105
Input Format
First and only argument is root node of the binary tree, A.
Output Format
Return an integer array denoting the preorder traversal of the given binary tree..
/**
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
vector<int> Solution::preorderTraversal(TreeNode* A) {
vector<int> vec;
stack<TreeNode*> stack;
stack.push(A);
while(!stack.empty())
{
TreeNode *curr=stack.top();
vec.push_back(curr->val);
stack.pop();
if(curr->right)
{
stack.push(curr->right);
}
if(curr->left)
{
stack.push(curr->left);
}
}
return vec;
}