Posted on 2014-01-11 02:54
Uriel 阅读(91)
评论(0) 编辑 收藏 引用 所属分类:
LeetCode
裸的二叉树先序遍历
注意判空树!!!
1 /**
2 * Definition for binary tree
3 * struct TreeNode {
4 * int val;
5 * TreeNode *left;
6 * TreeNode *right;
7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8 * };
9 */
10
11 vector<int> que;
12
13 void pre_order(TreeNode *root) {
14 if(root == NULL) return;
15 que.push_back(root->val);
16 if(root->left != NULL) pre_order(root->left);
17 if(root->right != NULL) pre_order(root->right);
18 }
19
20 class Solution {
21 public:
22 vector<int> preorderTraversal(TreeNode *root) {
23 if(!que.empty()) que.clear();
24 pre_order(root);
25 return que;
26 }
27 };