gpt4 book ai didi

c++ - 实现一个递归的Void函数(求二叉搜索树的高度)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:20:36 25 4
gpt4 key购买 nike

我需要实现一个 void 函数来计算二叉树中每个节点的高度并将其存储在每个节点中。我在网上找到了一些本质上是递归的解决方案,但它们返回的是 int。示例包括 ( https://www.geeksforgeeks.org/write-a-c-program-to-find-the-maximum-depth-or-height-of-a-tree/ )。模型答案之间的区别,除了它不是空函数之外,还在于它也不在每个节点中存储高度。

这是我对解决方案的尝试,但我似乎无法让代码正常工作,也无法重新调整模型答案以递归地应用于 void 函数。当我在辅助代码中运行我的代码进行测试时,它甚至没有显示任何输出。

void computeHeight(Node *n) {
Node* ltraverser = n;
Node* rtraverser = n;
int lheight = 0;
int rheight =0;

if (n == NULL) {
n->height = 0;
}

while (ltraverser->left != NULL) {
ltraverser = ltraverser->left;
lheight += 1;
}

while (rtraverser->right != NULL) {
rtraverser = rtraverser->right;
lheight += 1;
}

if (lheight > rheight) {
n->height = lheight;
}

else {
n->height = rheight;
}

computeHeight(n->left);
computeHeight(n->right);
}

供引用:

The starter code below defines a class called "Node" that has two child pointers ("left" , "right") and an integer "height" member variable. There is also a constructor Node() that initializes the children to nullptr and the height to -1.

/*
The height of a node is the number of edges in
its longest chain of descendants.

Implement computeHeight to compute the height
of the subtree rooted at the node n. Note that
this function does not return a value. You should
store the calculated height in that node's own
height member variable. Your function should also
do the same for EVERY node in the subtree rooted
at the current node. (This naturally lends itself
to a recursive solution!)

Assume that the following includes have already been
provided. You should not need any other includes
than these.

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>

You have also the following class Node already defined.
You cannot change this class definition, so it is
shown here in a comment for your reference only:

class Node {
public:
int height; // to be set by computeHeight()
Node *left, *right;
Node() { height = -1; left = right = nullptr; }
~Node() {
delete left;
left = nullptr;
delete right;
right = nullptr;
}
};
*/

用于测试代码

// This function prints the tree in a nested linear format.
void printTree(const Node *n) {
if (!n) return;
std::cout << n->height << "(";
printTree(n->left);
std::cout << ")(";
printTree(n->right);
std::cout << ")";
}

Node *n = new Node();
n->left = new Node();
n->right = new Node();
n->right->left = new Node();
n->right->right = new Node();
n->right->right->right = new Node();

computeHeight(n);

printTree(n);
std::cout << std::endl << std::endl;
printTreeVertical(n);

delete n;
n = nullptr;

return 0;
}

最佳答案

不是返回节点高度,而是在左右节点上递归调用 computeHeight,然后将最大高度存储在节点结构中。

#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <algorithm>

class Node {
public:
int height;
Node *left, *right;
Node() { height = -1; left = right = nullptr; }
~Node() {
delete left;
left = nullptr;
delete right;
right = nullptr;
}
};

void computeHeight(Node *node) {

if (node == nullptr) {
return;
}

computeHeight(node->left);
computeHeight(node->right);

int leftHeight = -1;
int rightHeight = -1;

if (node->left != nullptr) {
leftHeight = node->left->height;
}

if (node->right != nullptr) {
rightHeight = node->right->height;
}

node->height = std::max(leftHeight, rightHeight) + 1;
}

void printNode(Node *n, int level = 0) {
if (n == nullptr) {
return;
}

std::cout << std::string(level * 2, ' ') << "Height = " << n->height << "\n";

printNode(n->left, level + 1);
printNode(n->right, level + 1);
}


int main() {
Node *n = new Node();
n->left = new Node();
n->right = new Node();
n->right->left = new Node();
n->right->right = new Node();
n->right->right->right = new Node();


computeHeight(n);
printNode(n);
}

关于c++ - 实现一个递归的Void函数(求二叉搜索树的高度),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58492056/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com