[英]Preorder tree traversal function which returns an array of integers in C
I try to write a function which return an array of integers containing the node values of the binary tree in preorder that is a node value must appear before the values of its left and right child. 我尝试编写一个函数,该函数返回一个整数数组,该数组包含预先排序的二叉树的节点值,该节点值必须出现在其左右子节点的值之前。
if root is NULL, return NULL 如果root为NULL,则返回NULL
for every node left child comes before right child 对于每个节点,留下孩子来到右孩子之前
For example; 例如;
int *a = preorder(bt1);
for (i=0; i<3; i++)
printf("%d ", a[i]);
>2_1_3_
Here is my work, but it doesn't work, where could be the problem in my code? 这是我的工作,但它不起作用,我的代码中可能出现问题?
int* preorder(TreeNode *root) {
int *a = malloc(sizeof(int)*50);
int i=0;
if(root == NULL)
return NULL;
else {
if(root != NULL) {
a[i] = root->val;
i++;
preorder(root->left);
preorder(root->right);
return a;
}
}
}
You have two issues in the code: 您在代码中有两个问题:
preorder
preorder
之前 i
outside of preorder
to allow it to change between calls i
在preorder
之外,以允许它在不同的通话之间切换 One example is the following code: 一个例子是以下代码:
int *a = malloc(sizeof(int)*50);
int inx = 0;
preorder(bt1, a, &inx);
void preorder(TreeNode *root, int* a, int* inx) {
if(root == NULL)
return;
else {
if(root != NULL) {
a[*inx] = root->val;
*inx = *inx + 1;
preorder(root->left, a, inx);
preorder(root->right, a, inx);
}
}
}
In each recursive call of this function you will allocate: 在每次递归调用此函数时,您将分配:
int *a = malloc(sizeof(int)*50);
You need to allocate space for array once and then use that same array. 您需要为数组分配一次空间,然后使用相同的数组。 Same thing is for using i = 0. You need to use one counter.
同样的事情是使用i = 0.你需要使用一个计数器。
You may want create array in main
function, and then pass array as function argument. 您可能希望在
main
函数中创建数组,然后将数组作为函数参数传递。 Or you could use global array, and access it that way. 或者您可以使用全局数组,并以这种方式访问它。 Same thing for counter variable.
计数器变量也是一样的。
Note: I don't see point of memory allocation in your example. 注意:我没有在您的示例中看到内存分配点。 You should better use static array if you're sure that tree won't have more then
ARRAY SIZE
nodes. 如果您确定树不会有更多的
ARRAY SIZE
节点,那么最好使用静态数组。
With the wanted function signature of preorder()
a solution is not possible. 使用
preorder()
的有用函数签名,解决方案是不可能的。 Therefore you need a helper function for the root == NULL
case and a traversal function which a pointer to the current position in the array. 因此,您需要一个用于
root == NULL
情况的辅助函数和一个遍历函数,该函数指向数组中的当前位置。 It also returns a pointer to the next free slot in the array. 它还返回指向数组中下一个空闲槽的指针。 A solution could look like:
解决方案可能如下所示:
#include <stdio.h>
#include <malloc.h>
struct TreeNode {
int val;
struct TreeNode* left, * right;
};
int tree_size(/*struct TreeNode* tree*/) { return 7; }
int* preorder_(struct TreeNode* tn, int* v) {
*v++ = tn->val;
if (tn->left) v = preorder_(tn->left, v);
if (tn->right) v = preorder_(tn->right, v);
return v;
}
int* preorder(struct TreeNode* tn) {
if (tn) {
int* v = malloc(tree_size(/*tn*/) * sizeof(int));
preorder_(tn, v);
return v;
} else {
return NULL;
}
}
int main(void) {
// 4
// 2 5
// 1 3 6 7
struct TreeNode
left = {2, &{1}, &{3]},
right = {5, &{6}, &{7}},
root = {4, &left, &right};
int *v, i;
v = preorder(&root);
for (i = 0; i < tree_size(/*tn*/); i++) {
printf("%d ", v[i]); // 4 2 1 3 5 6 7
}
free(v);
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.