Skip to content

Commit

Permalink
Binary tree postorder traversal
Browse files Browse the repository at this point in the history
Signed-off-by: begeekmyfriend <[email protected]>
  • Loading branch information
begeekmyfriend committed Oct 18, 2017
1 parent 634d295 commit 6eabacf
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
2 changes: 2 additions & 0 deletions 145_binary_tree_postorder_traversal/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
all:
gcc -O2 -o test bst_postorder.c
76 changes: 76 additions & 0 deletions 145_binary_tree_postorder_traversal/bst_postorder.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <stdio.h>
#include <stdlib.h>

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

struct node_backlog {
struct TreeNode *parent;
struct TreeNode *right;
};

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

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

while (node != NULL || top != stack) {
if (node == NULL) {
nbl = *--top;
if (nbl.right != NULL) {
node = nbl.right;
nbl.right = NULL;
*top++ = nbl;
} else {
node = nbl.parent;
results[count++] = node->val;
node = NULL;
continue;
}
}
nbl.parent = node;
nbl.right = node->right;
*top++ = nbl;
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 = postorderTraversal(&root, &count);
for (i = 0; i < count; i++) {
printf("%d ", results[i]);
}
printf("\n");
return 0;
}

0 comments on commit 6eabacf

Please sign in to comment.