|

楼主 |
发表于 2020-4-7 22:37:29
|
显示全部楼层
- /**
- * Definition for a binary tree node.
- * struct TreeNode {
- * int val;
- * struct TreeNode *left;
- * struct TreeNode *right;
- * };
- */
- void step(struct TreeNode *root, int *result, int *rSize)
- {
- if(!root) return;
- result[*rSize] = root->val;
- (*rSize)++;
- step(root->left, result, rSize);
- step(root->right, result, rSize);
- }
- /**
- * Note: The returned array must be malloced, assume caller calls free().
- */
- int* preorderTraversal(struct TreeNode* root, int* returnSize){
- int *result = (int *)malloc(sizeof(int) * 1000);
- int rSize = 0;
- step(root, result, &rSize);
- *returnSize = rSize;
- return result;
- }
复制代码 执行结果:通过
显示详情
执行用时 :4 ms, 在所有 C 提交中击败了58.35%的用户
内存消耗 :6 MB, 在所有 C 提交中击败了100.00%的用户
|
|