提问人:user6703592 提问时间:9/18/2019 最后编辑:user6703592 更新时间:1/12/2020 访问量:659
在 C++ 中使用一个成员函数计算二叉树的高度
use one member function to calculate the height of a binary tree in C++
问:
这是计算二叉树高度的代码。我们假设所有节点值都是正 int。我使用了函数的递归。是否可以将成员函数组合成一个函数?对于一维情况(链表)很容易实现,但我对树一无所知。height
int height()
int height(TreeNode* root)
struct TreeNode
{
int val = 0;
TreeNode *left = NULL;
TreeNode *right = NULL;
TreeNode(int val): val(val){}
};
struct BinaryTree
{
TreeNode *root;
BinaryTree(TreeNode *root) : root(root) {}
int height() {
return height(root);
}
int height(TreeNode* root) {
if (!root)
{
return 0;
}
else
{
int lheight = height(root->left);
int rheight = height(root->right);
return lheight > rheight ? lheight + 1 : rheight + 1;
}
}
答:
3赞
Scott Hunter
9/18/2019
#1
我认为这就是你所要求的;它只是使用计算树在子树上的树自身高度的方法,而不是调用方法来计算任意树在子树上的高度。
int height() {
if (!root)
{
return 0;
}
else
{
int lheight = root->left ? root->left.height() : 0;
int rheight = root->right ? root->right.height() : 0;
return lheight > rheight ? lheight + 1 : rheight + 1;
}
}
更新:正如@Gupta所指出的,没有方法。我认为它应该,但这并没有使这个解决方案变得不那么不正确。TreeNode
height
这是一个可以添加到的版本:TreeNode
int height() {
int lheight = left ? left.height() : 0;
int rheight = right ? ight.height() : 0;
return std::max(lheight, rheight) + 1;
}
评论
1赞
SergeyA
9/18/2019
小建议:return std::max(lheight, rheight) + 1
2赞
TonySalimi
9/18/2019
但是没有方法。TreeNode
height()
0赞
user6703592
9/18/2019
对于,我有错误root->left.height()
TreeNode* BinaryTree::root expression must have class type
1赞
TonySalimi
9/18/2019
@user6703592 这就是我提到的。你应该将方法移动到你的结构中。height()
TreeNode
1赞
ggorlen
9/18/2019
#2
虽然它不那么简洁,但您可以使用显式堆栈将方法重写为迭代方法:
int height() {
std::stack<std::pair<TreeNode *, int> > node_stack;
node_stack.push(std::make_pair(root, 1));
int height = 0;
while (!node_stack.empty()) {
auto node = node_stack.top();
node_stack.pop();
if (node.first) {
height = std::max(height, node.second);
node_stack.push(std::make_pair(node.first->left, node.second + 1));
node_stack.push(std::make_pair(node.first->right, node.second + 1));
}
}
return height;
}
评论
1赞
Mike Vine
9/18/2019
TBH 此代码是正确的代码,因为它重新生成了递归。.它回答了这个问题。+1
2赞
SergeyA
9/18/2019
#3
从表面上回答问题。以下代码会将两个重载合并为一个:height
class BinaryTree {
inline static const TreeNode sentinel;
...
public:
int height(const TreeNode* root_in = &sentinel) const {
if (root_in == &sentinel)
return height(root);
if (!root_in)
return 0;
int lheight = height(root_in->left);
int rheight = height(root_in->right);
return std::max(lheight, rheight) + 1;
}
评论
height()
height(TreeNode* root)
height