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

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

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. php猴子吃桃

    <?php header("content-type:text/html;charset=utf-8"); /* 有一堆桃子,猴子第一天吃了其中的一半,并再多吃了一个! 以后 ...

  2. 【DB2】DB2使用IMPORT命令导入含有自增长列的表报错处理

    1.启动数据库:db2start 2.创建数据库:create db TestDB using codeset gbk territory CN  collate using identity 3.连 ...

  3. Xen on Ubuntu

    实验环境 ubuntu-14.04.1-desktop-amd64.iso Recommended reference: https://help.ubuntu.com/community/Xen h ...

  4. 【转载】aspx,ascx和ashx使用小结

    做asp.net开发的对.aspx,.ascx和.ashx都不会陌生.关于它们,网上有很多文章介绍."纸上得来终觉浅,绝知此事要躬行",下面自己总结一下做个笔记.1..aspxWe ...

  5. Spring 中StreamUtils教程

    本文我们介绍StreamUtils类使用.StreamUtils是spring中用于处理流的类,是java.io包中inputStream和outputStream,不是java8中Steam.使用时 ...

  6. HTML5 2D平台游戏开发#4状态机

    在实现了<HTML5 2D平台游戏开发——角色动作篇之冲刺>之后,我发现随着角色动作的增加,代码中的逻辑判断越来越多,铺天盖地的if() else()语句实在让我捉襟见肘: 这还仅仅是角色 ...

  7. linux uart驱动——uart platfrom 注册(三)

    一:注册platform device 注册一个platfrom device一般需要初始化两个内容,设备占用的资源resource和设备私有数据dev.platfrom_data.设备的resour ...

  8. js时间戳格式化成日期格式的多种方法

    js需要把时间戳转为为普通格式,一般的情况下可能用不到的, 下面先来看第一种吧 复制代码代码如下: function getLocalTime(nS) { return new Date(parseI ...

  9. RethinkDB创始人教你怎样找到创业点子

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemh1YmFpdGlhbg==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  10. excel表格定义导入到powerdesigner脚本

    打开powerdesigner,shift + ctrl + X 打开脚本窗口 输入执行的脚本,点 run 即可. 简单的导入Excel脚本 '开始 Option Explicit Dim mdl ' ...