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

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

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

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

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

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

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

代码1:超时

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. int tree[10010][2];
  7. int sum;
  8. int dfs(int i){
  9. if(tree[i][0]==0&&tree[i][1]==0) return 1;
  10. if(tree[i][0]&&!tree[i][1]) return sum=dfs(tree[i][0]);//只有左孩子
  11. if(!tree[i][0]&&tree[i][1]) return sum=dfs(tree[i][1]);//只有右孩子
  12. else
  13. if(dfs(tree[i][0])==dfs(tree[i][1])) return sum=dfs(tree[i][0])+1;//相等的时候,宽度为其中一个+1
  14. else return sum=dfs(tree[i][0])>dfs(tree[i][1])?dfs(tree[i][0]):dfs(tree[i][1]);//不等的时候,宽度为大的
  15. }
  16. int main(){
  17. int n;
  18. int i;
  19. int q;
  20. while(~scanf("%d",&n)){
  21. sum=0;
  22. memset(tree,0,sizeof(tree));
  23. for(i=2;i<=n;i++){
  24. scanf("%d",&q);
  25. if(tree[q][0]==0) tree[q][0]=i;
  26. else tree[q][1]=i;
  27. }
  28. int ans=dfs(1);
  29. printf("%d\n",ans);
  30. }
  31. return 0;
  32. }

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

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

  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. using namespace std;
  5.  
  6. int tree[10010][2];
  7. //int lsum,rsum;
  8. int dfs(int i){
  9. int lsum,rsum;//无语,,这俩变量定义为全局就不对,,求解脱
  10. if(tree[i][0]==0&&tree[i][1]==0) return 1;
  11. if(tree[i][0]&&!tree[i][1]){
  12. // cout<<"1只有左孩子"<<endl;
  13. return dfs(tree[i][0]);//只有左孩子
  14. }
  15. if(!tree[i][0]&&tree[i][1]){//此处代码不会运行,因为不存在只有右孩子的情况
  16. // cout<<"2只有右孩子"<<endl;
  17. return dfs(tree[i][1]);//只有右孩子
  18. }
  19.  
  20. lsum=dfs(tree[i][0]);
  21. rsum=dfs(tree[i][1]);
  22. //cout<<"lsum "<<lsum<<" rsum "<<rsum<<endl;
  23. if(lsum==rsum) {
  24. // cout<<"3左右孩子相等"<<endl;
  25. // cout<<"lsum+1 "<<lsum+1<<endl;
  26. return lsum+1;//相等的时候,宽度为其中一个+1
  27. }
  28. else {
  29. // cout<<"4左右孩子不相等"<<endl;
  30. return lsum>rsum?lsum:rsum;//不等的时候,宽度为大的
  31. }
  32. }
  33. int main(){
  34. int n;
  35. int i;
  36. int q;
  37. while(~scanf("%d",&n)){
  38. memset(tree,0,sizeof(tree));
  39. for(i=2;i<=n;i++){
  40. scanf("%d",&q);
  41. if(tree[q][0]==0) tree[q][0]=i;
  42. else tree[q][1]=i;
  43. }
  44. int ans=dfs(1);
  45. printf("%d\n",ans);
  46. }
  47. return 0;
  48. }

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. LeetCode 206 Reverse Linked List(反转链表)(Linked List)(四步将递归改写成迭代)(*)

    翻译 反转一个单链表. 原文 Reverse a singly linked list. 分析 我在草纸上以1,2,3,4为例.将这个链表的转换过程先用描绘了出来(当然了,自己画的肯定不如博客上面精致 ...

  2. java:注解(二)

    自定义注解 使用@interface自定义注解时,自动继承了java.lang.annotation.Annotation接口,由编译程序自动完成其他细节.在定义注解时,不能继承其他的注解或接口.@i ...

  3. ubuntu在terminal下安装mysql

    安装的时候.仅仅须要在terminal中输入下面几条命令 1.sudo apt-get install mysql-server 2.apt-get isntall mysql-client 3. s ...

  4. Oracle SQL性能优化 - 根据大表关联更新小表

    需求: 小表数据量20w条左右,大表数据量在4kw条左右,需要根据大表筛选出150w条左右的数据并关联更新小表中5k左右的数据. 性能问题: 对筛选条件中涉及的字段加index后,如下常规的updat ...

  5. React系列之--props属性

    版权声明:本文为博主原创文章,未经博主允许不得转载. PS:转载请注明出处作者:TigerChain地址:http://www.jianshu.com/p/fa81cebac3ef本文出自TigerC ...

  6. 进程间的八种通信方式----共享内存是最快的 IPC 方式

    1.无名管道( pipe ):管道是一种半双工的通信方式,数据只能单向流动,而且只能在具有亲缘关系的进程间使用.进程的亲缘关系通常是指父子进程关系. 2.高级管道(popen):将另一个程序当做一个新 ...

  7. 解决Oracle用户被锁定的方法

    解决Oracle用户被锁定的方法 1,cmd控制台: 使用sqlplus 命令:sqlplus sys/密码@ip/orcl as sysdba; 2,先设置具体时间格式,以便查看具体时间 SQL&g ...

  8. redis客户端连接,最大连接数查询与设置

    ##redis客户端连接数 redis通过监听一个TCP端口或socket的方式接收来自客户端的连接, 当与客户端建立连接后,redis内部会进行如下操作:()客户端socket会被设置为非阻塞模式, ...

  9. ASP.NET动态网站制作(12)-- JQ(4)

    前言:这节课接着上次课的继续讲. 内容:接上--> 1.jq元素样式设置:  (4)某个元素中是否含有某个css类别,返回布尔型:$("li:last").hasClass( ...

  10. Unity编辑器扩展之RequireComponent等详解

    RequireComponent的使用: 当你添加的一个用了RequireComponent组件的脚本,需要的组件将会自动被添加到game object(游戏物体).这个可以有效的避免组装错误.举个例 ...