Leetcoder 前序,中序,后序遍历代码

class Solution {
public:
	vector<int> postorderTraversal(TreeNode* root) {
		vector<int> vec;
		travel(root, vec);
		return vec;
	}
       //后序遍历
	void travel(TreeNode* root, vector<int>& v)
	{
		if (root == NULL)return;
		travel(root->left,v);
		travel(root->right, v);
		v.push_back(root->val); 
	}
       //前序遍历
	void pretravel(TreeNode* root, vector<int>& v)
	{
		if (root == NULL)return;
		travel(root->left,v);
		travel(root->right, v);
		v.push_back(root->val); 
	}        
       //中序遍历
	void midtravel(TreeNode* root, vector<int>& v)
	{
		if (root == NULL)return;
		travel(root->left,v);
		v.push_back(root->val); 
		travel(root->right, v);
	}     
};

  

posted @ 2017-05-04 14:57  风轻云淡走天涯  阅读(767)  评论(0编辑  收藏  举报