转自http://www.jianshu.com/p/e37495f72cf6

hihocoder 1342

解释:
题目描述了一种用ASCII码绘制的满二叉树,然后将树的根设置在一个特殊坐标轴的原点(0,0),坐标轴x向下为正向,y向右是正向。树的每个树枝与节点都占用1*1的大小。现在需要求在坐标轴中任意画一个矩形,里面会有多少个树的节点。例如样例输入中,对于(0,0)与(2,2)形成的矩形里面,包含有根节点和它的右叶子节点,所以输出的是2。

分析:
1、这是是一个二叉树的问题,肯定要构造树结构,为了简单,这里就声明一个Node的结构体,通过结构体指针来构建树。代码如下:

struct Node {
Node *lchild, *rchild;
long px, py;
Node(long _px, long _py)
{
lchild = rchild = NULL;
px = _px;
py = _py;
}
};

px,py是节点的坐标,lchild与rchild分别对应左右子节点。

2、接下里就是生成树,这里输入就是树的高度,我们就根据高度来生成满二叉树。生成的时候根据题目规则,我们需要注意树的树枝占位情况。通过分析我们可以得出,高度为1的节点,它一边的树枝数量是0,高度2的为1,高度3的为2,其它高度的节点树枝数量是其子节点数量的2倍加1。这样我们可以用个递归实现。代码如下:

long stickNumWithHeight(int height)
{
if (height == 1) {
return 0;
}
if (height == 2) {
return 1;
}
if (height == 3) {
return 2;
}
return stickNumWithHeight(height - 1) * 2 + 1;
} void buildTreeWithHeight(Node &node, int height)
{
if (height == 1) {
return;
}
long step = stickNumWithHeight(height) + 1;
node.lchild = new Node(node.px + step, node.py - step);
node.rchild = new Node(node.px + step, node.py + step);
buildTreeWithHeight(*node.lchild, height-1);
buildTreeWithHeight(*node.rchild, height-1);
}

3、树生成过后,我们只需要对每个矩形遍历检测这棵树,就可得到在当前矩形中节点数量,代码如下:

int checkNodeInArea(Node &node, int x1, int y1, int x2, int y2)
{
int sum = 0;
if (node.px >= x1 && node.py >= y1 && node.px <= x2 && node.py<= y2) {
sum += 1;
}
if (node.lchild != NULL) {
sum += checkNodeInArea(*node.lchild,x1,y1,x2,y2);
}
if (node.rchild != NULL) {
sum += checkNodeInArea(*node.rchild,x1,y1,x2,y2);
}
return sum;
}

完整代码:

#include <iostream>
#include <algorithm>
#include <map>
#include <string>
#include <string.h>
#include <ctype.h>
#include <vector>
using namespace std; struct Node
{
Node *lchild,*rchild;
long px,py;
Node(long _px,long _py)
{
lchild = rchild = NULL;
px=_px;
py=_py;
}
}; long stickNumWithHeight(int height)
{
if (height == 1) {
return 0;
}
if (height == 2) {
return 1;
}
if (height == 3) {
return 2;
}
return stickNumWithHeight(height - 1) * 2 + 1;
} void buildTreeWithHeight(Node &node,int height)
{
if(height==1)return;
long step = stickNumWithHeight(height)+1;
node.lchild = new Node(node.px+step,node.py-step);
node.rchild = new Node(node.px+step,node.py+step);
buildTreeWithHeight(*node.lchild,height-1);
buildTreeWithHeight(*node.rchild,height-1);
} int checkNodeInArea(Node &node, int x1, int y1, int x2, int y2)
{
int sum = 0;
if (node.px >= x1 && node.py >= y1 && node.px <= x2 && node.py<= y2) {
sum += 1;
}
if (node.lchild != NULL) {
sum += checkNodeInArea(*node.lchild,x1,y1,x2,y2);
}
if (node.rchild != NULL) {
sum += checkNodeInArea(*node.rchild,x1,y1,x2,y2);
}
return sum;
} int main(){
int N,M;
scanf("%d%d",&N,&M);
Node *root= new Node(0,0);
buildTreeWithHeight(*root,N);
while(M--)
{
int x1,x2,y1,y2;
scanf("%d %d %d %d",&x1,&y1,&x2,&y2);
int amout = checkNodeInArea(*root,x1,y1,x2,y2);
cout<<amout<<endl;
}
return 0;
}

hihocoder 1342 Full Binary Tree Picture【完全二叉树】的更多相关文章

  1. PAT 1110 Complete Binary Tree[判断完全二叉树]

    1110 Complete Binary Tree(25 分) Given a tree, you are supposed to tell if it is a complete binary tr ...

  2. leetcode_919. Complete Binary Tree Inserter_完全二叉树插入

    https://leetcode.com/problems/complete-binary-tree-inserter/ 给出树节点的定义和完全二叉树插入器类的定义,为这个类补全功能.完全二叉树的定义 ...

  3. [LeetCode] 919. Complete Binary Tree Inserter 完全二叉树插入器

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  4. [Swift]LeetCode919. 完全二叉树插入器 | Complete Binary Tree Inserter

    A complete binary tree is a binary tree in which every level, except possibly the last, is completel ...

  5. leetcode 958. Check Completeness of a Binary Tree 判断是否是完全二叉树 、222. Count Complete Tree Nodes

    完全二叉树的定义:若设二叉树的深度为h,除第 h 层外,其它各层 (1-h-1) 的结点数都达到最大个数,第 h 层所有的结点都连续集中在最左边,这就是完全二叉树. 解题思路:将树按照层进行遍历,如果 ...

  6. PAT A1110 Complete Binary Tree (25 分)——完全二叉树,字符串转数字

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  7. [二叉树建树&完全二叉树判断] 1110. Complete Binary Tree (25)

    1110. Complete Binary Tree (25) Given a tree, you are supposed to tell if it is a complete binary tr ...

  8. PAT甲级——1110 Complete Binary Tree (完全二叉树)

    此文章同步发布在CSDN上:https://blog.csdn.net/weixin_44385565/article/details/90317830   1110 Complete Binary ...

  9. PAT-1064 Complete Binary Search Tree(完全二叉树)

    A Binary Search Tree (BST) is recursively defined as a binary tree which has the following propertie ...

随机推荐

  1. 在windows中停止mysql提示:'服务正在启动或停止中,请稍候片刻后再试一次'

    发现mysql的windows服务异常,准备卸载并重新注册服务,输入: mysqld --remove MySQL 提示: 发现卸载不掉这个服务,于是找到MySQL服务的pid sc queryex ...

  2. 枚举专项练习_Uva725(Division)_Uva11059(Maximun Product)

    //Uva725 #include <iostream> #include <cstring> #include <cstdlib> #include <cs ...

  3. 第三周结对项目--小学生四则运算CAI软件汇报及总结(UI/web)

    前言: 这周是和我队友苏卫喜一起结对开发,我主要是写项目文档需求分析,她是通过我的需求文档来进行做思维导图,之后我们通过思维导图一起讨论用户界面设计. 以下就是我的需求分析1.0版本 1.   软件名 ...

  4. vue学习起步:了解下

    渐进式 有这么一句话,vue是渐进式框架. 抽取“渐进式框架”和“自底向上增量开发的设计”这两个概念是什么?中的解释: 渐进式代表的含义是:主张(主张指使用时的硬性要求)最少.来个对比就知道什么叫主张 ...

  5. python - class类 (四) 三大特性之一 :继承

    继承: #继承 #什么时候用继承? # 1.当类之间有显著的不同,并且较小的类是较大的类的所需的组建时,用组合比较好. # 2.当类之间有很多相同的功能,提取这些共同的功能做成基类,用继承比较好 # ...

  6. Struts自定义拦截器&拦截器工作原理

    0.拦截器的调用原理: 拦截器是一个继承了序列化接口的普通接口.其工作原理是讲需要被拦截的对象作为参数传到intercept()方法内,在方法内部对此对象进行处理之后再执行原方法.intercept( ...

  7. Spring+CXF整合来管理webservice(服务器启动发布webservice)

    Spring+CXF整合来管理webservice    实现步骤:      1. 添加cxf.jar 包(集成了Spring.jar.servlet.jar ),spring.jar包 ,serv ...

  8. 简单透彻理解JSONP原理及使用

    首先提一下JSON这个概念,JSON是一种轻量级的数据传输格式,被广泛应用于当前Web应用中.JSON格式数据的编码和解析基本在所有主流语言中都被实现,所以现在大部分前后端分离的架构都以JSON格式进 ...

  9. 在ASP.NET Web Forms中用System.Web.Optimization取代SquishIt

    将一个ASP.NET Web Forms项目从.NET Framework 4.0升级至.NET Framework 4.5之后,发现SquishIt竟然引发了HTTP Error 500.0 - I ...

  10. linux 同步IO: sync、fsync与fdatasync、sys_sync【转】

    本文转自:http://blog.csdn.net/cywosp/article/details/8767327 和 http://www.2cto.com/os/201204/126687.html ...