DS二叉树--左叶子数量
题目描述
计算一颗二叉树包含的叶子结点数量。
左叶子是指它的左右孩子为空,而且它是父亲的左孩子
提示:可以用三叉链表法,也可以用现有算法对两层结点进行判断
建树方法采用“先序遍历+空树用0表示”的方法
输入
第一行输入一个整数t,表示有t个测试数据
第二行起输入二叉树先序遍历的结果,空树用字符‘0’表示,输入t行
输出
逐行输出每个二叉树的包含的左叶子数量
样例输入
样例输出
#include<iostream>
#include<string>
using namespace std;
class BitreeNode
{
public:
char data;
BitreeNode *left;
BitreeNode *right;
BitreeNode():left(NULL),right(NULL){}
~BitreeNode(){}
};
class Bitree
{
private:
BitreeNode *Root;
int pos,count;
string strtree;
BitreeNode *CreateBitree();
void countleaves(BitreeNode *t);
public:
Bitree() { count = ; };
~Bitree() {};
void CreateTree(string TreeArray);
void countleaves();
};
void Bitree::CreateTree(string treearray)
{
pos = ;
strtree.assign(treearray);
Root = CreateBitree();
}
BitreeNode *Bitree::CreateBitree()
{
BitreeNode *T;
char ch;
ch = strtree[pos++];
if (ch == '')
T = NULL;
else
{
T = new BitreeNode();
T->data = ch;
T->left = CreateBitree();
T->right = CreateBitree();
}
return T;
}
void Bitree::countleaves()
{
countleaves(Root);
cout << count << endl;
}
void Bitree::countleaves(BitreeNode *t)
{
if (t)
{
if (t->left)
{
if (!t->left->left && !t->left->right)
count++;
}
countleaves(t->left);
countleaves(t->right);
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
string str;
cin >> str;
Bitree *tree;
tree = new Bitree();
tree->CreateTree(str);
tree->countleaves();
}
}
DS二叉树--左叶子数量的更多相关文章
- DS二叉树--叶子数量
题目描述 计算一颗二叉树包含的叶子结点数量. 提示:叶子是指它的左右孩子为空. 建树方法采用“先序遍历+空树用0表示”的方法,即给定一颗二叉树的先序遍历的结果为AB0C00D00,其中空节点用字符‘0 ...
- [Swift]LeetCode404. 左叶子之和 | Sum of Left Leaves
Find the sum of all left leaves in a given binary tree. Example: 3 / \ 9 20 / \ 15 7 There are two l ...
- 【leetcode 简单】 第九十四题 左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 # Definition for a binary ...
- 先序遍历创建二叉树,对二叉树统计叶子节点个数和统计深度(创建二叉树时#代表空树,序列不能有误)c语言
#include "stdio.h" #include "string.h" #include "malloc.h" #define NUL ...
- LeetCode: 404.左叶子节点
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父 ...
- LeetCode 404. 左叶子之和(Sum of Left Leaves)
404. 左叶子之和 404. Sum of Left Leaves LeetCode404. Sum of Left Leaves 题目描述 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 ...
- LC: 404.左叶子节点
计算给定二叉树的所有左叶子之和. 示例: / \ 9 20 / \ 15 7 ,所以返回 24 解析 我们需要找到这样的节点 属于叶子节点 属于父节点的左子节点 方法一:用栈,dfs遍历,用全局变量r ...
- LeetCode404Sum of Left Leaves左叶子之和
计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...
- Java实现 LeetCode 404 左叶子之和
404. 左叶子之和 计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9 20 / \ 15 7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 /** * Definiti ...
随机推荐
- [LeetCode&Python] Problem 888. Fair Candy Swap
Alice and Bob have candy bars of different sizes: A[i] is the size of the i-th bar of candy that Ali ...
- [MarkDown] markdown语法小结
目录 写在前面 目录 特殊字符自动转换 段落和换行 第一阶标题 第二阶标题显示效果有下划线 H1 H2有下划线 H3 区块引用 列表 代码区块 分割线 链接 强调 删除线 代码 图片 反斜杠 表格 g ...
- test-ipv6
http://test-ipv6.com/ ! 你的公网 IPv4 地址是 89.42.31.211! 你的公网 IPv6 地址是 2001:ac8:21:8::376e:989b! 你已接入 IPv ...
- 小米4c刷LineageOS
注意,本文仅限于小米4c,其他手机仅可参考步骤.如下rom,su,gapps包的下载都是小米4c的,深刷miflash也仅适用于小米手机.准备工作:请自行备份好手机内的个人资料. 电脑环境,usb驱动 ...
- KMP算法自我理解 和 模板
字符串 abcd abc abcd abc 匹配串 cdabcd 匹配串的 next 0 0 0 0 1 2: 开始匹配 abcd abc abcd abc cd abc d a,d 匹配失 ...
- Iterator迭代器快捷键
原文:https://blog.csdn.net/mingjie1212/article/details/51143444/ ★.iter 生成增强for:for (String s : locati ...
- LeetCode – Smallest Rotation with Highest Score
Given an array A, we may rotate it by a non-negative integer K so that the array becomes A[K], A[K+1 ...
- 【java多线程】java8的流操作api和fork/join框架
原文:https://blog.csdn.net/u011001723/article/details/52794455/ 一.测试一个案例,说明java8的流操作是并行操作 1.代码 package ...
- 深入浅出理解 COOKIE MAPPING
转载自:http://www.myttnn.com/digital-marketing/cookie-mapping-introduction/ 在RTB(实时竞价广告,Real-Time-Biddi ...
- Scala中的Map使用例子
Map结构是一种非常常见的结构,在各种程序语言都有对应的api,由于Spark的底层语言是Scala,所以有必要来了解下Scala中的Map使用方法. (1)不可变Map特点: api不太丰富 如果是 ...