forked from begeekmyfriend/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: begeekmyfriend <[email protected]>
- Loading branch information
1 parent
7e4e9b0
commit 4e051f3
Showing
2 changed files
with
47 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
all: | ||
gcc -O2 -o test bst_inorder_traversal.c |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
|
||
struct TreeNode { | ||
int val; | ||
struct TreeNode *left; | ||
struct TreeNode *right; | ||
}; | ||
|
||
static void traverse(struct TreeNode *node, int *result, int *count) | ||
{ | ||
if (node->left != NULL) { | ||
traverse(node->left, result, count); | ||
} | ||
|
||
result[*count] = node->val; | ||
(*count)++; | ||
|
||
if (node->right != NULL) { | ||
traverse(node->right, result, count); | ||
} | ||
} | ||
|
||
/** | ||
* * Return an array of size *returnSize. | ||
* * Note: The returned array must be malloced, assume caller calls free(). | ||
* */ | ||
int* inorderTraversal(struct TreeNode* root, int* returnSize) { | ||
if (root == NULL) { | ||
*returnSize = 0; | ||
return NULL; | ||
} | ||
int count = 0; | ||
int *result = malloc(5000 * sizeof(int)); | ||
traverse(root, result, &count); | ||
*returnSize = count; | ||
return result; | ||
} | ||
|
||
int main() | ||
{ | ||
int count = 0; | ||
inorderTraversal(NULL, &count); | ||
return 0; | ||
} |