【练习题】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 ...
随机推荐
- C#将string转换为十六进制
/// <summary> /// 将string格公式为十六进制数据 /// </summary> /// <param ...
- Android官方教程翻译(5)——设置ActionBar
Setting Up the Action Bar 设置Action Bar PREVIOUSNEXT THIS LESSONTEACHES YOU TO 这节课教你 1. Support An ...
- WinEdt && LaTex(五)—— 内容的排版
1. 无序列表 需要的环境是\begin{itemize} \end{itemize} \begin{itemize} \item hello \item world \end{itemize} 2. ...
- Matlab Tricks(二十四)—— 将一副图像逆时针旋转 180°
function I2 = rot180(I) I2 = I(end:-1:1, end:-1:1); % 上下颠倒,左右颠倒:
- Matlab随笔之分段线性函数化为线性规划
原文:Matlab随笔之分段线性函数化为线性规划 eg: 10x, 0<=x<=500 c(x)=1000+8x, 500<=x<=1000 300 ...
- eclipse 配置maven tomcat 环境
一 maven配置文件路径 二 tomcat 和JRE位置 三 validation 设置 四 五 六 设置run(debug) configurations 七 添加server时添加 reso ...
- UNITY VR 视频/图片 开发心得(二)
上回说到了普通的全景图片,这回讲真正的VR. 由于这种图片分为两部分,所以我们需要两个Camera对象以及两个球体.首先新建一个Camera对象,并将其命名为RightEye(其它名字也无妨,只要你自 ...
- 3D-Touch Home Screen Quick Actions 使用
1. 3D-Touch简单介绍 3D-Touch是iPhone 6s推出的一种可以让你与手机进行互动的全新方式.这一次,iPhone 能够感应你按压屏幕的力度.除了轻点.轻扫.双指开合这些熟悉的 Mu ...
- linux服务脚本编写
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 3 ...
- 零元学Expression Blend 4 - Chapter 27 PathlistBox被Selected时的蓝底蓝框问题
原文:零元学Expression Blend 4 - Chapter 27 PathlistBox被Selected时的蓝底蓝框问题 最近收到网友Cloud的来信,询问我有关放进PathlistBox ...