Skip to content

Commit

Permalink
BST preorder traversal
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Sep 27, 2017
1 parent 2416c12 commit 76417bc
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 144_binary_tree_preorder_traversal/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test bst_preorder.c
61 changes: 61 additions & 0 deletions 144_binary_tree_preorder_traversal/bst_preorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <stdio.h>
#include <stdlib.h>

struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};

/**
** Return an array of size *returnSize.
** Note: The returned array must be malloced, assume caller calls free().
**/
static int* preorderTraversal(struct TreeNode* root, int* returnSize) {
if (root == NULL) {
return NULL;
}

int cap = 10000, count = 0;
int *results = malloc(cap * sizeof(int));
struct TreeNode **stack = malloc(cap / 16 * sizeof(*stack));
struct TreeNode **top = stack;
struct TreeNode *node = root;

while (node != NULL || top != stack) {
if (node == NULL) {
node = *--top;
}

results[count++] = node->val;
if (node->right != NULL) {
*top++ = node->right;
}
node = node->left;
}

*returnSize = count;
return results;
}

int main(int argc, char **argv)
{
struct TreeNode root, node1, node2;
root.val = 1;
node1.val = 2;
node2.val = 3;
root.left = NULL;
root.right = &node1;
node1.left = &node2;
node1.right = NULL;
node2.left = NULL;
node2.right = NULL;

int i, count = 0;
int *results = preorderTraversal(&root, &count);
for (i = 0; i < count; i++) {
printf("%d ", results[i]);
}
printf("\n");
return 0;
}

0 comments on commit 76417bc

Please sign in to comment.