c++递归打印二叉树的问题
我一直在练习一些旧的 C++ 问题,为一些工作面试做准备,目前我正在尝试从数组递归构造二叉树,然后也递归地按顺序打印它。然而,当我尝试输出结果时,我得到了一些奇怪的值。
问题:从数组 [4,2,5,1,3] 构造二叉树,然后创建一个打印
的函数 它们递归地按顺序排列。
答案:我能够打印结果,但是我的解决方案包含一些意外的 0,这些 0 也会在结果中打印。我不知道这些 0 如何最终出现在打印结果中。
这是我当前的打印结果(注意值之间不需要的 0):0 1 0 2 0 3 0 4 0 5 0
这是我编写的 C++ 解决方案。 (只需复制并粘贴并在编译器上运行它):
#include <iostream>
using namespace std;
const int SIZE = 5;
struct node{
node *leftBranch;
node *rightBranch;
int val;
};
int data[SIZE] = {4,2,5,1,3};
node* construct_tree(int);
void print_tree(node*);
node * construct_tree(int num){
node *tmp = new node();
if(num < SIZE){
tmp->leftBranch = construct_tree(2*num + 1);
tmp->val = data[num];
tmp->rightBranch = construct_tree(2*num + 2);
}
return tmp;
}
int main(){
node *tree = construct_tree(0);
print_tree(tree);
return 0;
}
void print_tree(node* tree){
if(tree == NULL)
return;
print_tree(tree->leftBranch);
cout<<tree->val<<" ";
print_tree(tree->rightBranch);
}
我想我对 C++ 和递归有点生疏了..我希望你们能帮助我。thx
I have been practicing some old C++ problems to prepare for a few job interviews, and I am currently trying to recursively construct a binary tree from an array, and then print it inorder recursively as well. However, I got some weird values when trying to output the result.
Problem : construct binary tree from array [4,2,5,1,3], and then create a function that prints
them inorder recursively.
Answer : I am able to print the result, however my solution contains some unexpected 0's that also gets printed within the result. I dont have a clue how those 0's can end up being in the printed results..
Here is the printed result I currently have (notice the unwanted 0's between values) :0 1 0 2 0 3 0 4 0 5 0
Here is the c++ solution I have written. (Just copy and paste and run it on your compiler):
#include <iostream>
using namespace std;
const int SIZE = 5;
struct node{
node *leftBranch;
node *rightBranch;
int val;
};
int data[SIZE] = {4,2,5,1,3};
node* construct_tree(int);
void print_tree(node*);
node * construct_tree(int num){
node *tmp = new node();
if(num < SIZE){
tmp->leftBranch = construct_tree(2*num + 1);
tmp->val = data[num];
tmp->rightBranch = construct_tree(2*num + 2);
}
return tmp;
}
int main(){
node *tree = construct_tree(0);
print_tree(tree);
return 0;
}
void print_tree(node* tree){
if(tree == NULL)
return;
print_tree(tree->leftBranch);
cout<<tree->val<<" ";
print_tree(tree->rightBranch);
}
I think I have been a little rusty with c++ and recursion..I hope you guys can help me.thx
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题出在construct_tree上。对它的调用是:
问题是,对construct_tree的每次调用都会创建一个添加到树中的新节点,即使
num
超出范围也是如此。The problem is in construct_tree. The calls to it are:
The problem is, every call to construct_tree creates a new node that is added to your tree, even when
num
is out of range.特德是对的。尝试按如下方式更改construct_tree:-
<代码>
节点 *tmp = null;
if(num < 大小)
{
tmp=新节点();
……
}
返回tmp;
Ted is right. Try changing construct_tree as follows :-
node *tmp = null;
if(num < SIZE)
{
tmp= new node();
......
}
return tmp;
你还有另一个问题。对树进行排序的算法高度依赖于您访问数据的顺序。尝试您的解决方案
You have another problem. Your algorithm for ordering the tree is highly dependent on the order in which you visit the data. Try your solution on