aboutsummaryrefslogtreecommitdiff
path: root/binary_tree_inorder.c
blob: f72a23d19229642d1d24216efb9213e99ba3c9c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
// 94. Binary Tree Inorder Traversal
#include "leetcode.h"

/*
 * given the root of a binary tree, return the inorder traversal of its nodes
 * values
 */

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

void travel(struct TreeNode *root, int *res, int *size) {
  if (!root)
    return;
  travel(root->left, res, size);
  res[(*size)++] = root->val;
  travel(root->right, res, size);
}

int *inorderTraversal(struct TreeNode *root, int *return_size) {
  *return_size = 0;
  int *ans = malloc(sizeof(int) * 100);
  travel(root, ans, return_size);
  return ans;
}