Leetcode: Binary Tree Inorder Traversal

中序遍历


代码:

#include <iostream>
#include <vector>
using namespace std;

 struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;
     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 };

class Solution {
public:
	vector<int> record;
	void inorderTravel(TreeNode *root) {
		if(root->left)
			inorderTravel(root->left);
		record.push_back(root->val);
		if(root->right)
			inorderTravel(root->right);
	}
    vector<int> inorderTraversal(TreeNode *root) {
		record.clear();
		inorderTravel(root);
		return record;
    }
};

  

posted @ 2013-12-05 16:45  SangS  阅读(162)  评论(0编辑  收藏  举报