二叉搜索树的深度
depth of a binary search tree
所以我需要用c++写一个函数来返回树的深度。我对这需要做什么有点困惑。它是每个单独节点的深度,还是整个树的深度,例如树有4个层次。如有任何帮助,不胜感激
树的深度是最深节点的级别。这看起来是一个很好的定义。话虽如此,这里有一个c++类的实现,其中root是类的一个属性。基本上,你得到左子树的深度和右子树的深度,然后选择两者中最大的一个。
#define max(a,b) ((a)>=(b) ? (a) : (b))
int height2(Node *t) {
if(!t)
return 0;
int height_left = height2(t->L);
int height_right = height2(t->R);
return 1 + max(height_left,height_right);
};
int height() {
return height2(root);
};
class Node {
public:
//...
unsigned int depth() {
return 1 + max(depth(left),
depth(right));
}
private:
unsigned int depth(Node* node) {
return node ? node->depth() : 0;
}
Node* left;
Node* right;
};
相关文章:
- 有根的二进制搜索树.保留与其父级的链接
- 正在尝试重载二进制搜索树分配运算符
- 使用C++创建特殊的二叉搜索树
- 在递归二叉搜索树中搜索
- 在二叉搜索树中插入时出现分段错误
- C++二叉搜索树模板从函数返回节点
- 我可以在没有堆栈的情况下在二叉搜索树中实现迭代器吗?
- 在二叉搜索树C++中计算平均值
- 在字符串的二叉搜索树中搜索子字符串 - C++
- 在二叉搜索树中插入新元素
- 如何打印在二叉搜索树中找到的数据?
- 查找存储在二叉搜索树的所有非叶子中的数据总和?(返回整数的独立递归函数
- 这是一个二叉搜索树吗?黑客排名问题
- 二叉搜索树 深度复制和取消引用
- C++二叉搜索树打印和深度计算
- 显示属于二叉搜索树深度路径的节点
- 深度优先搜索树遍历实现在C++
- 深度优先搜索树边分类
- 二叉搜索树的深度
- 二进制搜索树的最大深度