aboutsummaryrefslogtreecommitdiff
path: root/construct_bt_io_po.c
diff options
context:
space:
mode:
Diffstat (limited to 'construct_bt_io_po.c')
-rw-r--r--construct_bt_io_po.c37
1 files changed, 37 insertions, 0 deletions
diff --git a/construct_bt_io_po.c b/construct_bt_io_po.c
new file mode 100644
index 0000000..9c145eb
--- /dev/null
+++ b/construct_bt_io_po.c
@@ -0,0 +1,37 @@
+// 106. Construct Binary Tree from Inorder and Postorder Traversal
+#include <stdint.h>
+#include <stdlib.h>
+
+/*
+ * given two integer arrays 'inorder' and 'postorder', where 'inorder' is the
+ * inorder traversal of a binary tree, and 'postorder' is the postorder
+ * traversal of the same tree, construct and return the binary tree.
+ */
+
+struct TreeNode {
+ int val;
+ struct TreeNode *left;
+ struct TreeNode *right;
+};
+
+struct TreeNode *buildTree(int *inorder, int inorder_size, int *postorder,
+ int postorder_size) {
+ if (inorder_size == 0)
+ return NULL;
+ struct TreeNode *root = (struct TreeNode *)malloc(sizeof(struct TreeNode));
+ root->val = postorder[postorder_size - 1];
+ root->left = NULL;
+ root->right = NULL;
+ if (postorder_size == 1)
+ return root;
+ uint16_t i;
+ for (i = 0; i < inorder_size; i++)
+ if (inorder[i] == root->val)
+ break;
+ int l_size = i;
+ root->left = buildTree(inorder, l_size, postorder, l_size);
+ int r_size = postorder_size - 1 - l_size;
+ root->right =
+ buildTree(&inorder[1 + l_size], r_size, &postorder[l_size], r_size);
+ return root;
+}