我试图用这段代码找到二叉树的高度,但它一直返回 0,有人能告诉我为什么吗?
I am trying to find the height of a Binary tree with this code, but it keeps returning 0, can someone tell me why?
我试图用这段代码找到二叉树的高度,但它一直返回 0,有人能告诉我为什么吗?
int heightHelper(Node* root, int maxheight, int rootheight)
{
if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
if (maxheight < rootheight) {
maxheight = rootheight;
}
}
else { //if it has then increase height by 1
rootheight += 1;
if (root->left != nullptr) {
heightHelper(root->left, maxheight, rootheight);
}
if (root->right != nullptr) {
heightHelper(root->right, maxheight, rootheight);
}
}
return maxheight; //return height
}
int height(Node* root)
{
// Write your code here.
return heightHelper(root, 0, 0); //root node base case
}
您混淆了两种做事方式。 使用指针或地址(int*
和 &maxheight
)传递结果参数。在这种情况下,没有 return 值。但是老实说,这是我主要只在硬件编程中看到的一种编码风格。
否则你可以return一个结果。您已经有一个 return 参数,只需修复该值的实际用法,因为您的代码会忽略它。
这里有两种方法:
使用return参数:
int heightHelper(Node* root, int height)
{
if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
return height;
}
else { //if it has then increase height by 1
height += 1;
int maxheight1;
int maxheight2;
if (root->left != nullptr) {
maxheight1 = heightHelper(root->left, height);
}
if (root->right != nullptr) {
maxheight2 = heightHelper(root->right, height);
}
// return maximum of the two
if (maxheight1 > maxheight2) return maxheight1;
else return maxheight2;
}
}
int height(Node* root)
{
// Write your code here.
return heightHelper(root, height); //root node base case
}
使用指针参数。请注意,rootheight 对于每个分支都是本地的,因此不要将其作为指针传递。
void heightHelper(Node* root, int* maxheight_ptr, int rootheight)
{
if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
if (*maxheight < rootheight) {
*maxheight = rootheight;
}
}
else { //if it has then increase height by 1
rootheight += 1;
if (root->left != nullptr) {
heightHelper(root->left, maxheight_ptr, rootheight);
}
if (root->right != nullptr) {
heightHelper(root->right, maxheight_ptr, rootheight);
}
}
}
int height(Node* root)
{
// Write your code here.
int maxheight = 0;
heightHelper(root, &maxheight, 0); //pass the addresses of variables
return maxheight;
}
我试图用这段代码找到二叉树的高度,但它一直返回 0,有人能告诉我为什么吗?
int heightHelper(Node* root, int maxheight, int rootheight)
{
if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
if (maxheight < rootheight) {
maxheight = rootheight;
}
}
else { //if it has then increase height by 1
rootheight += 1;
if (root->left != nullptr) {
heightHelper(root->left, maxheight, rootheight);
}
if (root->right != nullptr) {
heightHelper(root->right, maxheight, rootheight);
}
}
return maxheight; //return height
}
int height(Node* root)
{
// Write your code here.
return heightHelper(root, 0, 0); //root node base case
}
您混淆了两种做事方式。 使用指针或地址(int*
和 &maxheight
)传递结果参数。在这种情况下,没有 return 值。但是老实说,这是我主要只在硬件编程中看到的一种编码风格。
否则你可以return一个结果。您已经有一个 return 参数,只需修复该值的实际用法,因为您的代码会忽略它。
这里有两种方法:
使用return参数:
int heightHelper(Node* root, int height)
{
if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
return height;
}
else { //if it has then increase height by 1
height += 1;
int maxheight1;
int maxheight2;
if (root->left != nullptr) {
maxheight1 = heightHelper(root->left, height);
}
if (root->right != nullptr) {
maxheight2 = heightHelper(root->right, height);
}
// return maximum of the two
if (maxheight1 > maxheight2) return maxheight1;
else return maxheight2;
}
}
int height(Node* root)
{
// Write your code here.
return heightHelper(root, height); //root node base case
}
使用指针参数。请注意,rootheight 对于每个分支都是本地的,因此不要将其作为指针传递。
void heightHelper(Node* root, int* maxheight_ptr, int rootheight)
{
if (root->right == nullptr && root->left == nullptr) { //checks if the node has a child
if (*maxheight < rootheight) {
*maxheight = rootheight;
}
}
else { //if it has then increase height by 1
rootheight += 1;
if (root->left != nullptr) {
heightHelper(root->left, maxheight_ptr, rootheight);
}
if (root->right != nullptr) {
heightHelper(root->right, maxheight_ptr, rootheight);
}
}
}
int height(Node* root)
{
// Write your code here.
int maxheight = 0;
heightHelper(root, &maxheight, 0); //pass the addresses of variables
return maxheight;
}