算法刷题 DAY14

144.前序遍历,145.后序遍历,94.中序遍序

//不可用全局变量,不然leetcode输出会有问题
/**
 * 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 pre_order(struct TreeNode* root,int *res,int *index){

    if(root==NULL) return;
    
    res[(*index)++]=root->val;

    pre_order(root->left,res,index);
    pre_order(root->right,res,index);

}


int* preorderTraversal(struct TreeNode* root, int* returnSize){

    int *res=(int*)calloc(10000,sizeof(int));
    *returnSize=0;

    pre_order(root,res,returnSize);

    return res;
    
}

你可能感兴趣的:(算法,java,数据结构,c语言)