Given a binary tree, return the inorder traversal of its nodes' values.
For example:
Given binary tree {1,#,2,3},
1
\
2
/
3
return [1,3,2].
递归方法,教科书上的。好像还可以用栈来解决这个问题。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
void traverse(TreeNode *root, vector<int> &results) {
if(root == NULL) return;
traverse(root->left, results);
results.push_back(root->val);
traverse(root->right, results);
}
vector<int> inorderTraversal(TreeNode *root) {
vector<int> results;
traverse(root, results);
return results;
}
};
3149

被折叠的 条评论
为什么被折叠?



