-
Notifications
You must be signed in to change notification settings - Fork 3
/
Binary Tree Inorder Traversal
51 lines (41 loc) · 1.23 KB
/
Binary Tree Inorder Traversal
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* struct TreeNode *left;
* struct TreeNode *right;
* };
*/
/**
* Note: The returned array must be malloced, assume caller calls free().
*/
int* inorderTraversal(struct TreeNode* root, int* returnSize){
*returnSize = 0;
// Check if the root is NULL
if (root == NULL) {
return NULL;
}
// Create an array to store the result (assuming a maximum of 100 nodes)
int* result = (int*)malloc(100 * sizeof(int));
// Initialize the index to 0
int index = 0;
struct TreeNode* stack[100]; // Stack to perform iterative traversal
int top = -1;
struct TreeNode* current = root;
while (current != NULL || top != -1) {
while (current != NULL) {
// Push the current node onto the stack
stack[++top] = current;
current = current->left;
}
// Process the current node
current = stack[top--];
result[index++] = current->val;
// Move to the right subtree
current = current->right;
}
// Update the returnSize to the actual size of the result array
*returnSize = index;
// Return the result array
return result;
}