Binary Tree inOrder Traversal

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

	}
};

  

posted @ 2014-07-01 13:33  dupuleng  阅读(61)  评论(0编辑  收藏  举报