【练习题】proj1 判断二叉树子树和是否为指定的值
2024-09-01 01:26:04
#include <stdio.h>
#include <vector>
#include <list>
#include<iostream>
using namespace std; struct BinaryTree{
int weight;
struct BinaryTree *left,*right;
};
int subtree_count(const struct BinaryTree *root,int m){
int temp_val;
static int count = ;
static int result=;
int left=;
int right=;
int flag=;
if(count == ){
flag =;
}
count ++;
if(root==NULL){
return ;
} if(root->left!=NULL){
left = subtree_count(root->left,m);
}
if(root->right!=NULL){
right = subtree_count(root->right,m);
}
temp_val = left + right+root->weight;
if(temp_val == m)
result++;
if(flag)
return result;
else
return temp_val;
} int main()
{
int input=;
//cin >> input;
BinaryTree *root1 = new BinaryTree;
root1->weight=; BinaryTree *root2 = new BinaryTree;
root2->weight=;
BinaryTree *root3 = new BinaryTree;
root3->weight=;
BinaryTree *root4 = new BinaryTree;
root4->weight=;
BinaryTree *root5 = new BinaryTree;
root5->weight=;root4->right=NULL; root1->left=root2;
root1->right=root3;
root2->left=root4;
root2->right=root5;
root4->right=NULL;
root4->left=NULL;
root5->right=NULL;
root5->left=NULL;
root3->right=NULL;
root3->left=NULL;
cout <<subtree_count(root1,input);
return ;
}
最新文章
- jdk源码分析红黑树——插入篇
- Spring IoC容器初始化过程学习
- WebApi服务Uri加密及验证的两种方式
- mysql 连接超时解决
- serialize-and-deserialize-bst
- QFrame好像是万能的(可以随意画线,或者图片,放在其它元素之间做点缀,还可OnClick)
- Linux多线程编程的时候怎么查看一个进程中的某个线程是否存活
- phalcon——闪存消息
- Sqoop葵花宝典
- 自主学习python文本进度条及π的计算
- redis 3.2 新数据结构:quicklist、String的embstr与raw编码方式分界点
- JavaEE 要懂的小事:三、图解Session(会话)
- 跨平台、跨语言应用开发,Elements 介绍
- Sethi model
- JS实现选择菜单栏(配合慕课网淘宝搜索框的课程)
- SDN 第三次作业
- cojs 强连通图计数1-2 题解报告
- 2018.08.11 洛谷P3224 [HNOI2012]永无乡(线段树合并)
- CRFPP/CRF++编译安装与部署
- PyQt4菜单栏