ZOJ 3805 Machine(二叉树,递归)
题意:一颗二叉树,求 “ 宽度 ”
思路:递归,貌似这个思路是对的,先记下,但是提交时超时,
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(二叉树,递归)的更多相关文章
- zoj 3805 Machine
Machine Time Limit: 2 Seconds Memory Limit: 65536 KB In a typical assembly line, machines are c ...
- Java - 二叉树递归与非递归
树的定义具有递归特性,因此用递归来遍历比较符合特性,但是用非递归方式就比较麻烦,主要是递归和栈的转换. import java.util.Stack; /** * @author 李文浩 * @ver ...
- (二叉树 递归) 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 ...
- (二叉树 递归) 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 ...
- (二叉树 递归) 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 ...
- (二叉树 递归) leetcode 144. Binary Tree Preorder Traversal
Given a binary tree, return the preorder traversal of its nodes' values. Example: Input: [1,null,2,3 ...
- JAVA二叉树递归构造、二叉树普通遍历及递归遍历
二叉树类: package com.antis.tree; public class BinaryTree { int data; //根节点数据 BinaryTree left; //左子树 Bin ...
- 如何求先序排列和后序排列——hihocoder1049+洛谷1030+HDU1710+POJ2255+UVA548【二叉树递归搜索】
[已知先序.中序求后序排列]--字符串类型 #1049 : 后序遍历 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho在这一周遇到的问题便是:给出一棵二叉树的前序和 ...
- java 二叉树递归遍历算法
//递归中序遍历 public void inorder() { System.out.print("binaryTree递归中序遍历:"); inorderTraverseRec ...
随机推荐
- iOS常用的加密方式
MD5 iOS代码加密 创建MD5类,代码如下 #import <Foundation/Foundation.h> @interface CJMD5 : NSObject +(NSStri ...
- python判断字符串类型
s为字符串 s.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False.(重点,这是字母数字一起判断的!!) s.isalpha() 所有字符都是字母,为真返回 Tur ...
- session 购物车
package session; import java.io.IOException;import java.util.ArrayList;import java.util.List; import ...
- 有一个投篮游戏。球场有p个篮筐,编号为0,1...,p-1。每个篮筐下有个袋子,每个袋子最多装一个篮球。有n个篮球,每个球编号xi 。规则是将数字为xi 的篮球投到xi 除p的余数为编号的袋里。若袋里已有篮球则球弹出游戏结束输出i,否则重复至所有球都投完。输出-1。问游戏最终的输出是什么?
// ConsoleApplication5.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<vector> ...
- Tsung 初步介绍安装
tsung是erlang的一个开源的一个压力测试工具,可以测试包括HTTP, WebDAV, Mysql, PostgreSQL, LDAP, and XMPP/Jabber等服务器.针对 HTTP ...
- Python学习总结之五 -- 入门函数式编程
函数式编程 最近对Python的学习有些怠慢,最近的学习态度和学习效率确实很不好,目前这种病况正在好转. 今天,我把之前学过的Python中函数式编程简单总结一下,分享给大家,也欢迎并感谢大家提出意见 ...
- Android:实现两个Activity相互切换而都不走onCreate()
本文要实现的目的是: 有3个Activity: A,B,C.从A中能够进入B,B中能够进入C.而且B和C之间可能须要多次相互切换,因此不能使用普通的startActivity-finish方式,由于又 ...
- 解决UICollectionView的Cell复用引起的布局混乱问题
解决UICollectionView的Cell复用引起的布局混乱问题 问题复现.gif 查了一下度娘发现没有好的解决办法,于是发动自己的聪明才智,终于找到如下解决办法(充分证明了自己动手丰衣足食啊
- urllib与urllib2的学习总结(python2.7.X): python urllib与urllib2
https://www.cnblogs.com/wly923/archive/2013/05/07/3057122.html
- H2 database 操作操作内存表
本例开发工具为 NetBeans,使用b2前提安装jdk. 第一步:在官网下载驱动包 :http://www.h2database.com ,本例版本为: h2-1.4.192.jar 第二步:安装开 ...