题目描述:

Given a binary tree, return the preorder traversal of its nodes’ values.

For example: Given binary tree {1,#,2,3},

1
2
3
4
5
6
1
\
2
/
3

return [1,2,3].

Note: Recursive solution is trivial, could you do it iteratively?

二叉树非递归前序遍历. 需要背下来的代码了…

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode *root) {
vector<TreeNode*> path;
vector<int> ret;
if(root == NULL) return ret;
TreeNode *node = root;

while(node || !path.empty()){
if(node){
ret.push_back(node->val);
path.push_back(node);
node = node->left;
}
else{
node = path.back();
path.pop_back();
node = node->right;
}
}

return ret;
}
};