前序遍历
class Solution {
public:
vector<int> result;
void getNode(TreeNode *root){
result.push_back(root->val);
if(root->left != NULL){
getNode(root->left);
}
if(root->right != NULL){
getNode(root->right);
}
}
vector<int> preorderTraversal(TreeNode *root) {
// write your code here
if(root == NULL){return result;}
getNode(root);
vector<int> *p = &result;
return *p;
}
};
中序遍历
void getNode(TreeNode *root){
if(root->left != NULL){
getNode(root->left);
}
result.push_back(root->val);
if(root->right != NULL){
getNode(root->right);
}
}
后序遍历
void getNode(TreeNode *root){
if(root->left != NULL){
getNode(root->left);
}
if(root->right != NULL){
getNode(root->right);
}
result.push_back(root->val);
}