是否可以把树上结点的编号,然后把二叉树存储在数组中呢?很遗憾如果结点在一条链上,那将是2^256个结点 所以需要采用动态结构 首先要读取结点,建立二叉树addnode()+read_input()承担这样的工作 然后遍历二叉树,读取结点编号输出bfs() 这道题有内存池应用的背景 附链接  http://blog.csdn.net/shawngucas/article/details/6574863 #include <cstdio> #include <cstring> #inc…
题意: 输入一颗二叉树,按照(左右左右, 节点的值)的格式.然后从上到下从左到右依次输出各个节点的值,如果一个节点没有赋值或者多次赋值,则输出“not complete” 一.指针方式实现二叉树 首先定义一个结构体,然后定义一个结构体指针root,作为整棵树的根节点.如果需要用到左右节点则申请一个空间,也就是用不到的就不申请,以节省空间. 遍历方式是广度优先遍历(BFS),从根节点依次拓展子节点,如果有子节点就入队,然后根节点出队.继续拓展,直到队列为空,即遍历完整棵树. 因为指针丢失以后会造成…
题意: 给定一颗树, 按层次遍历输出. 分析: 用数组模拟二叉树, bfs即可实现层次遍历 #include <bits/stdc++.h> using namespace std; struct Node{ bool have_value; int v, left, right; void reset(){//用于再次初始化节点 init(); } void init(){ have_value = ; v = left = right = ; } Node():have_value(),l…
题目链接:https://vjudge.net/problem/UVA-122 题目大意:输入一颗二叉树,你的任务是按从上到下,从左到右的顺序输出各个结点的值.每个结点都按照从根节点到它的移动序列给出(L表示左,R表示右) 在输入中,每个结点的左括号 和右括号之间没有空格,相邻结点之间用一个空格隔开.每棵树的输入用一对空括号)()结束  入上图所示: 注意:如果从根结点到某个叶节点的路径有的结点没有在输入中给出,或者给出超过一次,输出not complete.  结点个数不超过256 思路:显然…
题目描述: 题目思路: 1.用结构链表来建树 2.用队列来实现层次遍历,当遍历到根节点时,将其子节点压入队列 #include <iostream> #include <cstdlib> #include <cstring> #include <vector> #include <queue> using namespace std; //树结点 struct Node{ int v ; Node* left,*right ; int have_…
直接层次遍历是比较简单的,但是题目要求的分层打印,这就变得稍微有些麻烦 我是采用两个队列的方法实现. 1.将树结构入队列1. 2.当队列1和队列2都不为空的时候,则一直循环. 3.当队列1不为空的时候,就一直取出队列1中的元素,打印其根节点数据,然后将其子树存入队列2,直到队列1为空结束. 4.与3相反. #!/usr/bin/env python3 import queue class Node(object): def __init__(self, data, left=None, righ…
Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to right, then right to left for the next level and alternate between). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 retur…
Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 return its bottom-up level or…
[抄题]: 给出一棵二叉树,返回其节点值的锯齿形层次遍历(先从左往右,下一层再从右往左,层与层之间交替进行) [思维问题]: 不知道反复切换要怎么做:用boolean normalOrder当作布尔型控制变量 [一句话思路]: [输入量]:空: 正常情况:特大:特小:程序里处理到的特殊情况:异常情况(不合法不合理的输入): [画图]: root一个点正常出入.第二层先左后右,出来就变成先右后左了. [一刷]: 层遍历是放在原来的queue中,但是zigzag遍历的左右节点是放在下一层,另外一个栈…
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 给定一个二叉树,返回他的层次遍历的节点的values.(提示,从左到右,一层一层的遍历) 例如: 给定一个二叉树 {1,#,2,3}, 1 \ 2 / 3 返回的层次遍历的结果是: [ [3], [9,20], [15,7] ] ++++++++++++++++++++++++++++++++++++++++++++++++++++…
103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). LeetCode103. Binary Tree Zigzag Level Order Traversal中等 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回锯齿形层次遍历如…
树状结构在计算机科学的许多领域中都相当重要.本问题牵涉到建立树及走访树.给你一二叉树,你的任务是写一个程序来打印依「阶层(level-order)」走访的结果.在本问题中,二叉树的每个节点含有一个正整数,并且节点的数目最少1个,最多256个.在「阶层」走访中,依阶层从低到高,同阶层从左到右的次序来打印.例如以下的二叉树阶层走访的结果为:5,4,8,11,13,4,7,2,1 在本问题中,二叉树以节点来表示.每个节点以一对(n,s)来表示.n代表此节点的值,s为一字串,代表从根节点到达此节点的路径…
非常不熟练  照着书大的 晚上尝试一下自己打  了解二叉树  用数组打 第一次: #include<bits/stdc++.h> using namespace std; bool failed; void addnode(int v,char *s); ]; struct node { bool flag; int v; node *left,*right; node():flag(false),left(NULL),right(NULL){} }; node *root; node *ne…
直接上代码... (另外也可以在递归的时候统计最优解,不过程序稍微复杂一点) #include <iostream> #include <string> #include <sstream> using namespace std; const int maxn=10000+10; int in_order[maxn],post_order[maxn],lch[maxn],rch[maxn]; int n; bool read_list(int *a){//输入一行用空…
看着lrj的代码自己敲了一遍,还没调试成功.... 有时间再进行完善 /* 状态start到各个状态u1,u2,u3..... 的倒水量分别为u1.dist,u2.dist,u3.dist.... */ /* 按倒水量递增的顺序去寻找当前状态的v[0],v[1],v[2]是否为d 但是程序实现的时候我们寻找目标水量是根据 ans[d]>0,而不是对每一个状态的v[]进行遍历 为什么这样做呢?当然是因为麻烦,ans[d]>0很直接地说明了v[]有三者之一为d */ #include <cs…
题目: 给出一棵树的表示,判断这棵树是否输入正确,如果正确就按层次遍历输出所有的结点,错误的话就输出not complete. 思路: 根据字符串中树的路径先将树建起来,在增加结点和层次遍历树的时候判断这棵树是不是输入正确. 错误的两种情况: 1.同一个结点被输入的两次 2.这个结点的孩子有值,但这个结点没有被输入值. 判断方法: 根据字符串中给出的路径,将整棵树先建起来,每个结点中设一个是否被访问过的标志vis 增加结点时如果这个结点的vis已经为true则为错误的情况1. 遍历树的时候,如果…
前序遍历 [144] Binary Tree Preorder Traversal 递归遍历 使用递归,先保存父节点的值,再对左子树进行遍历(递归),最后对右子树进行遍历(递归) vector<int> preorderTraversal(TreeNode* root) { vector<int> re; preTraversal(root, re); return re; } void preTraversal(TreeNode* root, vector<int>&…
102. 二叉树的层次遍历 102. Binary Tree Level Order Traversal 题目描述 给定一个二叉树,返回其按层次遍历的节点值. (即逐层地,从左到右访问所有节点). 每日一算法2019/5/11Day 8LeetCode102. Binary Tree Level Order Traversal 例如: 给定二叉树: [3,9,20,null,null,15,7], 3 / \ 9 20 / \ 15 7 返回其层次遍历结果: [ [3], [9,20], [15…
[题意] 给定一棵树每个结点的权重和路径(路径用LR串表示),输出这棵树的层次遍历 [思路] 注意输入输出,sscanf用来格式化地截取需要的数据,strchr来在字符串中查找字符的位置 [Accepted] #include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<queue> using n…
题目链接: https://cn.vjudge.net/problem/UVA-122 /* 问题 给出每个节点的权值和路线,输出该二叉树的层次遍历序列. 解题思路 根据输入构建链式二叉树,再用广度优先遍历保存权值最后输出. */ #include<cstdio> #include<cstring> #include<vector> #include<queue> using namespace std; ; bool failed; struct NODE…
题意:给定结点值和从根结点到该结点的路径,若根到某个叶结点路径上有的结点输入中未给出或给出超过一次,则not complete,否则层次遍历输出所有结点. 分析:先建树,建树的过程中,沿途结点都申请了内存,所以在bfs的时候如果该结点有内存,但是未赋值,那就算not complete,别忘了释放内存,虽然这不影响AC,但是注意内存泄漏是好习惯. PS: strchr函数原型:char * strchr(char * str, int ch); 功能就是找出在字符串str中第一次出项字符ch的位置…
Trees are fundamental in many branches of computer science. Current state-of-the art parallel computers such as Thinking Machines’ CM-5 are based on fat trees. Quad- and octal-trees are fundamental to many algorithms in computer graphics. This proble…
 Trees on the level UVA - 122  解题思路: 首先要解决读数据问题,根据题意,当输入为“()”时,结束该组数据读入,当没有字符串时,整个输入结束.因此可以专门编写一个readin()函数,类型设置为bool型,遇到第一种情况时返回true,遇到第二种情况返回false,主程序中只要发现readin返回false时就break,结束整个大循环. 接下来要建立二叉树,首先为二叉树编写一个结构体,然后根据字符串的输入情况来建树,如果是‘L’就往左走,走不动时建一颗新树,同样…
UVA.122 Trees on the level(二叉树 BFS) 题意分析 给出节点的关系,按照层序遍历一次输出节点的值,若树不完整,则输出not complete 代码总览 #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <queue> #include <vector> #define nmax 10…
题目给出一个后缀表达式,让你求从下往上的层次遍历. 思路:结构体建树,然后用数组进行BFS进行层次遍历,最后把数组倒着输出就行了. uva过了,poj老是超时,郁闷. 代码: #include <cstdio> #include <cstring> #include <cstdlib> const int maxn = 10001; char str[maxn]; int p; struct Node { char data; Node* l; Node* r; };…
题目链接:https://vjudge.net/contest/209862#problem/B 题目大意: Trees on the level Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 591    Accepted Submission(s): 200 Problem Description Trees are fundame…
Trees are fundamental in many branches of computer science (Pun definitely intended). Current state- of-the art parallel computers such are based on fat trees . Quad- and octal-trees are fundamental to many algorithms in computer graphics. This probl…
1.二叉树的建立 首先,定义数组存储树的data,然后使用list集合将所有的二叉树结点都包含进去,最后给每个父亲结点赋予左右孩子. 需要注意的是:最后一个父亲结点需要单独处理 public static TreeNode root; //建立二叉树内部类 class TreeNode{ public Object data; //携带变量 public TreeNode lchild,rchild; //左右孩子 public TreeNode() { data = null; lchild…
03-树1. List Leaves (25) Given a tree, you are supposed to list all the leaves in the order of top down, and left to right. Input Specification: Each input file contains one test case. For each case, the first line gives a positive integer N (<=10) wh…
#include<stdio.h> #include<stdlib.h> #include<string.h> #define num 100 #define OK 1 #define ERROR 0 #define OVERFLOW -1 #define FALSE 0 #define TRUE 1 typedef int Status; typedef char DataType; typedef struct node { DataType data; struc…