Skip to content

Commit

Permalink
BST inorder traversal
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Aug 17, 2017
1 parent 7e4e9b0 commit 4e051f3
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 094_binary_tree_inorder_traversal/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test bst_inorder_traversal.c
45 changes: 45 additions & 0 deletions 094_binary_tree_inorder_traversal/bst_inorder_traversal.c
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;
}

0 comments on commit 4e051f3

Please sign in to comment.