分别计算左右孩子的二进制树
Count binary tree left and right children separately
我的代码仅计算二进制树的正确子女的数量。
int count(Arbin<T> a){
if(a.isEmpty()) return 0;
int num_l=0, num_r=0;
if(!a.leftChild.isEmpty())
num_l = count(a.leftChild());
if(!a.rightChild.isEmpty())
num_r = count(a.rightChild())+1;
return num_l+num_r;}
如何修改它,以便我可以分别获得合适的孩子的数量和左派孩子的数量?
Arbin
类仅允许我进行此操作(而且我无法修改):
-
rightChild()
:返回正确的孩子的节点 -
leftChild()
:返回左儿童的节点 -
isEmpty()
:返回节点是否为空
如果您可以修改count()
,请执行以下操作:
void count(Arbin<T> a, int& l, int& r){
if(a.isEmpty()) return ;
if(!a.leftChild.isEmpty())
count(a.leftChild(), l+1, r);
if(!a.rightChild.isEmpty())
count(a.rightChild(), l, r+1);
}
现在将count
称为以下
int l = 0, r = 0;
count(tree, l, r);
// here you've left child count in `l` and right count in `r`
相关文章:
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 有根的二进制搜索树.保留与其父级的链接
- 多态二进制函数
- 正在读取二进制文件(is_open)
- visual在c++中将十进制数转换为二进制数
- C++十进制到二进制,如何转换
- cpp二进制搜索问题,计算给定数组中输入元素的出现次数
- 二进制搜索树叶数问题
- 如何将一个ostringstream十六进制字符串字符对转换为单个unit8t等价的二进制值
- 为什么二进制搜索在我的测试中不起作用
- 重载==不适用于二进制树
- 正在尝试重载二进制搜索树分配运算符
- 在C++中将类(带有Vector成员)保存为二进制文件
- 如何从二进制文件中读取字符串
- 使用不同的CRT将新的C++代码与旧的(二进制)组件隔离开来的最佳方法是什么
- 带有数组键C++的二进制映射
- 如何将二进制格式的 C++ 对象的 std::vector 保存到磁盘?
- 二进制数之和(使用C样式字符串)
- 分别计算左右孩子的二进制树
- 从二进制搜索树中删除只有一个孩子的节点