题意:一颗二叉树,求  “  宽度  ”

思路:递归,貌似这个思路是对的,先记下,但是提交时超时,

1.如果当前节点只有左孩子,那么当前宽度等于左孩子宽度

2.如果当前节点只有右孩子,那么当前宽度等于右孩子宽度

3.如果当前节点既有左孩子又有孩子

3.1两个孩子宽度相等,则当前宽度等于其中一个孩子宽度+1

3.2两个孩子宽度不等,则当前宽度等于两个孩子宽度中大的那一个

代码1:超时

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int tree[10010][2];
int sum;
int dfs(int i){
if(tree[i][0]==0&&tree[i][1]==0) return 1;
if(tree[i][0]&&!tree[i][1]) return sum=dfs(tree[i][0]);//只有左孩子
if(!tree[i][0]&&tree[i][1]) return sum=dfs(tree[i][1]);//只有右孩子
else
if(dfs(tree[i][0])==dfs(tree[i][1])) return sum=dfs(tree[i][0])+1;//相等的时候,宽度为其中一个+1
else return sum=dfs(tree[i][0])>dfs(tree[i][1])?dfs(tree[i][0]):dfs(tree[i][1]);//不等的时候,宽度为大的
}
int main(){
int n;
int i;
int q;
while(~scanf("%d",&n)){
sum=0;
memset(tree,0,sizeof(tree));
for(i=2;i<=n;i++){
scanf("%d",&q);
if(tree[q][0]==0) tree[q][0]=i;
else tree[q][1]=i;
}
int ans=dfs(1);
printf("%d\n",ans);
}
return 0;
}

代码2:正确,代码1的超时原因是,在递归过程中,递归运算太多了,多了不少重复的运算

但是,以下正确代码更是蛋疼,有两个变量不能定义为全局变量,,,,啊啊啊啊啊,为啥啊,,,

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; int tree[10010][2];
//int lsum,rsum;
int dfs(int i){
int lsum,rsum;//无语,,这俩变量定义为全局就不对,,求解脱
if(tree[i][0]==0&&tree[i][1]==0) return 1;
if(tree[i][0]&&!tree[i][1]){
// cout<<"1只有左孩子"<<endl;
return dfs(tree[i][0]);//只有左孩子
}
if(!tree[i][0]&&tree[i][1]){//此处代码不会运行,因为不存在只有右孩子的情况
// cout<<"2只有右孩子"<<endl;
return dfs(tree[i][1]);//只有右孩子
} lsum=dfs(tree[i][0]);
rsum=dfs(tree[i][1]);
//cout<<"lsum "<<lsum<<" rsum "<<rsum<<endl;
if(lsum==rsum) {
// cout<<"3左右孩子相等"<<endl;
// cout<<"lsum+1 "<<lsum+1<<endl;
return lsum+1;//相等的时候,宽度为其中一个+1
}
else {
// cout<<"4左右孩子不相等"<<endl;
return lsum>rsum?lsum:rsum;//不等的时候,宽度为大的
}
}
int main(){
int n;
int i;
int q;
while(~scanf("%d",&n)){
memset(tree,0,sizeof(tree));
for(i=2;i<=n;i++){
scanf("%d",&q);
if(tree[q][0]==0) tree[q][0]=i;
else tree[q][1]=i;
}
int ans=dfs(1);
printf("%d\n",ans);
}
return 0;
}

ZOJ 3805 Machine(二叉树,递归)的更多相关文章

  1. zoj 3805 Machine

    Machine Time Limit: 2 Seconds      Memory Limit: 65536 KB In a typical assembly line, machines are c ...

  2. Java - 二叉树递归与非递归

    树的定义具有递归特性,因此用递归来遍历比较符合特性,但是用非递归方式就比较麻烦,主要是递归和栈的转换. import java.util.Stack; /** * @author 李文浩 * @ver ...

  3. (二叉树 递归) leetcode 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  4. (二叉树 递归) leetcode 105. Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  5. (二叉树 递归) leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  6. (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal

    Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...

  7. JAVA二叉树递归构造、二叉树普通遍历及递归遍历

    二叉树类: package com.antis.tree; public class BinaryTree { int data; //根节点数据 BinaryTree left; //左子树 Bin ...

  8. 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】

    [已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和 ...

  9. java 二叉树递归遍历算法

    //递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...

随机推荐

  1. TCP/IP详解 卷一(第二章 链路层)

    在TCP/IP协议族中,链路层主要有三个目的: 1.为IP模块发送和接收IP数据报 2.为ARP模块发送ARP请求和接收ARP应答 3.为RARP请求和接收RARP应答 TCP/IP支持多种不同的链路 ...

  2. DevOps企业实践与架构

    原文地址:http://www.sohu.com/a/112351816_355140 什么是DevOps及其误区 DevOps概念从2009年提出已有8个年头.可是在8年前的那个时候,为什么DevO ...

  3. webapi设置一个Action同时支持get和post请求

    代码如下: [AcceptVerbs("GET", "POST")] public HttpResponseMessage Http([FromUri]Prox ...

  4. UML类图简明教程

    作者:郭孝星 微博:郭孝星的新浪微博 邮箱:allenwells@163.com 博客:http://blog.csdn.net/allenwells Github:https://github.co ...

  5. Erlang节点重启导致的incarnation问题(转)

    转自霸爷的博客: 转载自系统技术非业余研究 本文链接地址: Erlang节点重启导致的incarnation问题 遇到个问题, =ERROR REPORT==== 10-Mar-2016::09:44 ...

  6. mongodb的IO测试工具 mongoperf

    之前没发现mongoperf这个工具,测试IO的状态用的是iostat来进行观察. mongoperf < myjsonconfigfile  echo "{nThreads:2,fi ...

  7. 基于zookeeper或redis实现分布式锁

    前言 在分布式系统中,分布式锁是为了解决多实例之间的同步问题.例如master选举,能够获取分布式锁的就是master,获取失败的就是slave.又或者能够获取锁的实例能够完成特定的操作. 目前比较常 ...

  8. C# WinForm退出方法

    1.this.Close();   只是关闭当前窗口,若不是主窗体的话,是无法退出程序的,另外若有托管线程(非主线程),也无法干净地退出: 2.Application.Exit();  强制所有消息中 ...

  9. 九度OJ 1072:有多少不同的面值组合? (计数)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3112 解决:1591 题目描述: 某人有8角的邮票5张,1元的邮票4张,1元8角的邮票6张,用这些邮票中的一张或若干张可以得到多少种不同的 ...

  10. jquery获取form表单中的内容,并将表单内容更新到datagrid的一行

    //执行不刷新页面更新所修改的行 var arr = $('#patient_form').serializeArray();//将表单中的数据格式化成数组 var m = new Array(); ...