【练习题】proj1 判断二叉树子树和是否为指定的值

#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 ;
}
【练习题】proj1 判断二叉树子树和是否为指定的值的更多相关文章
- jquery里判断数组内是否包含了指定的值或元素的方法
本文讲的是在jquery里,如何判断一个数组里是否包含了指定的值,变量,或其它对象元素的方法. 在jquery里,我们可以用$.inArray来判断一个数组里是否包含了指定的值或其它对象元素,来看一个 ...
- js判断一个数组是否包含一个指定的值
今天看了一下 有好几种方法 总结一下 1:array.indexOf 此方法判断数组中是否存在某个值,如果存在返回数组元素的下标,否则返回-1 let arr = ['something', ...
- 判断一个数组是否包含一个指定的值 includes-ES6
var array1 = [1, 2, 3]; console.log(array1.includes(2)); // trueconsole.log(array1.includes(2, 5)); ...
- 56. 2种方法判断二叉树是不是平衡二叉树[is balanced tree]
[本文链接] http://www.cnblogs.com/hellogiser/p/is-balanced-tree.html [题目] 输入一棵二叉树的根结点,判断该树是不是平衡二叉树.如果某二叉 ...
- 【剑指offer】判断二叉树是否为平衡二叉树
2013-09-03 14:16:51 面试题39:求二叉树的深度.判断二叉树是否为平衡二叉树 小结: 根据平衡二叉树的定义,需要判断每个结点,因此,需要遍历二叉树的所有结点,并判断以当前结点为根的树 ...
- 判断二叉树是否BST
一.问题: 请实现一个函数,检查一棵二叉树是否为二叉查找树.给定树的根结点指针TreeNode* root,请返回一个bool,代表该树是否为二叉查找树. 二.思路: 解法一:从根节点开始遍历二叉树, ...
- PAT-1119(Pre- and Post-order Traversals)+前序和后序遍历确定二叉树+判断二叉树是否唯一
Pre- and Post-order Traversals PAT-1119 这题难度较大,主要需要考虑如何实现根据前序遍历和后序遍历来确定一颗二叉树 一篇好的文章: 题解 import java. ...
- 【easy】110. Balanced Binary Tree判断二叉树是否平衡
判断二叉树是否平衡 a height-balanced binary tree is defined as a binary tree in which the depth of the two su ...
- [Leetcode 100]判断二叉树相同 Same Tree
[题目] 判断二叉树是否相同. [思路] check函数. p==null并且q==null,返回true;(两边完全匹配) p==null或q==null,返回false;(p.q其中一方更短) p ...
随机推荐
- WPF 在image控件用鼠标拖拽出矩形
原文:WPF 在image控件用鼠标拖拽出矩形 版权声明:博客已迁移到 http://lindexi.gitee.io 欢迎访问.如果当前博客图片看不到,请到 http://lindexi.gitee ...
- Swift类和结构
类和结构有非常多的共同点: 定义属性存储数据 定义方法执行功能处理 定义下标,通过下标訪问他们的值 初始化他们的状态 通过扩展(Extension)扩展其功能 遵守协议(Protocol).协议提供一 ...
- eclipse中让关闭的项目不再显示
- Ubuntu logomaker sh: 1: pngtopnm: not found 解决方案
暂时未找到logomaker的方法,来解决 命令替换,在文件夹: pngtopnm open_show.png > temp.ppm ppmquant 224 temp.ppm >temp ...
- MAT 专题
http://smallnetvisitor.iteye.com/blog/1826434 运行user任务管理器查看到的pid号:
- error C2220: 警告被视为错误 - 没有生成“object”文件
原文:error C2220: 警告被视为错误 - 没有生成"object"文件 这种错误的原因是:原因是该文件的代码页为英文,而我们系统中的代码页为中文. 解决方案: 1. ...
- Swagger 生成API文档
1.打开程序包管理控制台输入: Install-Package Swashbuckle 2.打开App_Start文件夹下的SwaggerConfig.cs文件找到 c.IncludeXmlComme ...
- jquery 用json设置css
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- GDI+ Bitmap与WPF BitmapImage的相互转换
原文:GDI+ Bitmap与WPF BitmapImage的相互转换 using System.Windows.Interop; //... // Convert BitmapImage to Bi ...
- requirejs教程(一):基本用法
介绍 RequireJS是一个非常小巧的JavaScript模块载入框架,是AMD规范最好的实现者之一.最新版本的RequireJS压缩后只有14K,堪称非常轻量.它还同时可以和其他的框架协同工作,使 ...