首页 > 代码库 > Binary Tree Preorder Traversal

Binary Tree Preorder Traversal

class Solution{public:	vector<int>preorderTraversal(TreeNode *root)	{		vector<int>preOrder;		Traversal(root,preOrder);		return preOrder;	}	stack<TreeNode*> st;private:	void Traversal(TreeNode *root,vector<int>&preOrder)	{		if(root==NULL)			return;		TreeNode *p;		TreeNode *pre=NULL;		bool flag=true;		p=root;		while(p||!st.empty())		{			if(p)			{				st.push(p);				preOrder.push_back(p->val);				p=p->left;			}			else			{				p=st.top();				st.pop();				p=p->right;			}		}	}};