二叉树的前后中遍历顺序:其实是父节点处理的顺序,
——例如:前序遍历就是父结点然后左到右
递归:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
void preOder(struct TreeNode* root, int* ret, int *returnSize){
if(root == NULL)
return;
ret[(*returnSize)++] = root->val;
preOder(root->left, ret, returnSize);
preOder(root->right, ret, returnSize);
}
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* ret = (int*)malloc(sizeof(int)*100);
*returnSize = 0;
preOder(root, ret, returnSize);
return ret;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
void postOder(struct TreeNode* root, int* ret, int* returnSize){
if(root == NULL)
return;
postOder(root->left, ret, returnSize);
postOder(root->right,ret, returnSize);
ret[(*returnSize)++] = root->val;
}
int* postorderTraversal(struct TreeNode* root, int* returnSize) {
int* ret = (int*)malloc(sizeof(int)*100);
*returnSize = 0;
postOder(root,ret, returnSize);
return ret;
}
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
void inOder(struct TreeNode* root, int* ret, int* returnSize){
if(root == NULL)
return;
inOder(root->left, ret, returnSize);
ret[(*returnSize)++] = root->val;
inOder(root->right, ret, returnSize);
}
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
int* ret = (int*)malloc(sizeof(int)*100);
*returnSize = 0;
inOder(root, ret, returnSize);
return ret;
}
迭代:
前序(因为是处理父结点,所以指针一直指向左边,到了尽头了,再返回处理)
int* preorderTraversal(struct TreeNode* root, int* returnSize) {
int* res = malloc(sizeof(int) * 2000);
*returnSize = 0;
if (root == NULL) {
return res;
}
struct TreeNode* stk[2000];
struct TreeNode* node = root;
int stk_top = 0;
while (stk_top > 0 || node != NULL) {
while (node != NULL) {
res[(*returnSize)++] = node->val;
stk[stk_top++] = node;
node = node->left;
}
node = stk[--stk_top];
node = node->right;
}
return res;
}
后序
int *postorderTraversal(struct TreeNode *root, int *returnSize) {
int *res = malloc(sizeof(int) * 2001);
*returnSize = 0;
if (root == NULL) {
return res;
}
struct TreeNode **stk = malloc(sizeof(struct TreeNode *) * 2001);
int top = 0;
struct TreeNode *prev = NULL;
while (root != NULL || top > 0) {
while (root != NULL) {
stk[top++] = root;
root = root->left;
}
root = stk[--top];
if (root->right == NULL || root->right == prev) {
res[(*returnSize)++] = root->val;
prev = root;
root = NULL;
} else {
stk[top++] = root;
root = root->right;
}
}
return res;
}
中序
int* inorderTraversal(struct TreeNode* root, int* returnSize) {
*returnSize = 0;
int* res = malloc(sizeof(int) * 501);
struct TreeNode** stk = malloc(sizeof(struct TreeNode*) * 501);
int top = 0;
while (root != NULL || top > 0) {
while (root != NULL) {
stk[top++] = root;
root = root->left;
}
root = stk[--top];
res[(*returnSize)++] = root->val;
root = root->right;
}
return res;
}