The Falling Leaves Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description   Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly b…
0.唔.这道题 首先要明确根节点在哪儿 初始化成pos=maxn/2; 1.因为是先序的输入方法,所以这个建树的方法很重要 void build(int p) { int v; cin>>v; ) { sum[p]+=v; build(p-),build(p+); } } #include <iostream> #include <cstring> using namespace std; ; int sum[maxn]; int k; void build(int p…
UVa 699 The Falling Leaves(递归建树) 假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和UVa 839 -- Not so Mobile(树的递归输入)有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #…
题目链接:https://vjudge.net/problem/UVA-1525 题目链接:https://vjudge.net/problem/POJ-1577 题目大意 略. 分析 建树,然后先序遍历. 代码如下 #include <cmath> #include <ctime> #include <iostream> #include <string> #include <vector> #include <cstdio> #i…
UVA.699 The Falling Leaves (二叉树 思维题) 题意分析 理解题意花了好半天,其实就是求建完树后再一条竖线上的所有节点的权值之和,如果按照普通的建树然后在计算的方法,是不方便实现的,思维转换,在数组中实现,从根节点,向左的话即在a[root-1]的位置累加上节点权值,再向左即在a[root-2]位置累加权值,向右则在a[root+1]的位置累加权值,以此类推,方可实现求和的要求. 代码总览 #include <cstdio> #include <cstring&…
本文纯属原创.转载请注明出处,谢谢. http://blog.csdn.net/zip_fan. Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the sa…
java创建二叉树并递归遍历二叉树前面已有讲解:http://www.cnblogs.com/lixiaolun/p/4658659.html. 在此基础上添加了非递归中序遍历二叉树: 二叉树类的代码: package binarytree; import linkedstack.LinkStack; import linkqueue.LinkQueue; public class BinaryTree { class Node { public Object data; public Node…
[145-Binary Tree Postorder Traversal(二叉树非递归后序遍历)] [LeetCode-面试算法经典-Java实现][全部题目文件夹索引] 原题 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 \ 2 / 3 return [3,2,1]. Note: Recursive sol…
Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on thetrees, followed quickly by the falling leaves accumulating under the trees. If the same thing happenedto binary trees, how large would the piles of…
  // UVa699 The Falling Leaves // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值之和.按照递归方式输入,-1表示空 树 // UVa699 The Falling Leaves // Rujia Liu // 题意:给一棵二叉树,每个节点都有一个水平位置:左儿子在它左边1个单位,右儿子在右边1个单位.从左向右输出每个水平位置的所有结点的权值之和.按照递归方式输入,-1表示空树…
HDU 3791 Falling Leaves 二叉搜索树  Figure 1Figure 1 shows a graphical representation of a binary tree of letters. People familiar with binary trees can skip over the definitions of a binary tree of letters, leaves of a binary tree, and a binary search tr…
[UVA699]The Falling Leaves 算法入门经典第6章例题6-10(P159) 题目大意:有一颗二叉树,求水平位置的和. 试题分析:乱搞就可以过,将树根节点的pos记为0,向左-1,向右+1,统计答案即可. #include<iostream> #include<cstring> #include<cstdio> #include<vector> #include<queue> #include<stack> #in…
uva 699 紫书P159 Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large w…
Description Each year, fall in the North Central region is accompanied by the brilliant colors of the leaves on the trees, followed quickly by the falling leaves accumulating under the trees. If the same thing happened to binary trees, how large woul…
题目描述 翻转一棵二叉树. 示例 输入: / \ / \ / \ 输出: / \ / \ / \ 题目要求 /** * Definition for a binary tree node. * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ struct TreeNode* invertTree(struct TreeNode* root){ } 题解 /** * De…
题目链接:http://acm.hust.edu.cn/vjudge/problem/19244 #include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #includ…
题意:对于每一棵树,每一个结点都有它的水平位置,左子结点在根节点的水平位置-1,右子节点在根节点的位置+1,从左至右输出每个水平位置的节点之和 解题思路:由于上题所示的遍历方式如同二叉树的前序遍历,与天平那题不同,本题不需要构造出完整的结点左右子树,只需要构造出结点的相对位置,每次输入一个结点树,若为-1,则返回,否则依次递归执行input(p-1)与input(p+1). 代码如下: #include<stdio.h> #include<cstring> #include<…
题意: 按先序方式输入一棵二叉树,节点是带权的,左孩子在父节点的左一个单位,右孩子在父节点的右一个单位,从左到右输出相同水平位置节点之和. 分析: 做了好几道二叉树的题,代码应该也很好理解了.这里maxn开始设了200.500都RE,后来索性开了2000,AC了 紫书上面init函数最后应该加一句 return true; //#define LOCAL #include <iostream> #include <cstdio> #include <cstring> u…
题意:给你一颗二叉树的前序遍历,空子树以-1表示,将左右子树的权值投影到一维数轴上,左儿子位置为根位置-1,右儿子+1求个个整点上的和: 题解:递归,整个过程只需维护一个sum数组. 更新根,更新leftson ,更新rightson; 代码: #define _CRT_SECURE_NO_WARNINGS #include "stdio.h" #include<stdio.h> #include<algorithm> #include<string>…
本来看着挺难的.大概是由于我多瞟了一眼题解,瞬间认为简单多了.做题就得这样,多自己想想.如今是 多校联赛,然而我并不会做. .. .慢慢来,一直在努力. 分析: 题上说了做多不会超过80行.所以能够开一个数组.这里我是把根节点作为第42个数,能够在建树的同一时候求 出那一列全部数值的和左孩子节点减一,右孩子节点加一.. .写的时候中间出了点小bug,忘了给flag重置0了,调 了好久.. . 第一次提交wa了,由于没有换行,题目要求结果之间有一行空行的. . . 贴代码: #include<st…
题意:给出按先序输入的一颗二叉树,分别求出从左到右的相同横坐标上的节点的权值之和 递归建树,然后用sum数组分别统计每一个横坐标上的权值之和 感觉建树都在递归递归递归= =慢慢理解吧 #include<iostream> #include<cstdio> #include<cstring> #include <cmath> #include<algorithm> using namespace std; typedef long long LL;…
二叉树的创建 思路:数组中从上到下依次放着二叉树中的元素,使用递归很容易实现,那么这里使用容器来存放之前的状态实现循环创建二叉树. TreeNode* createTree(int *arr, int length) { ) return NULL; TreeNode* root = ]); deque<pair<TreeNode*,int> > route; route.push_back(make_pair(root,)); while(!route.empty()) { Tr…
今天是LeetCode专题第60篇文章,我们一起来看的是LeetCode的94题,二叉树的中序遍历. 这道题的官方难度是Medium,点赞3304,反对只有140,通过率有63.2%,在Medium的题目当中算是很高的了.这题非常基础,可以说是程序员必会的算法题之一. 我们先来看题意. 题意 题意很短, 只有一句话,给定一棵二叉树,返回它中序遍历的结果. 样例 Input: [1,null,2,3]   1    \     2    /   3Output: [1,3,2] 用递归做这道题非常…
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA3QAAAMsCAIAAACTL3d2AAAgAElEQVR4nOx9y7GuPA4tKRADk/92T8mBEIiBFMiADMiABC7zvsW4qxh3FRmQg+9gFSpt2Rbm9T321hqcOpsPbFkvC1nYmTMYDAaDwWAwGO7A//k//yd7Nw0Gg8FgMBgMhl+Cf//73xZcGgwGg8FgMBjugWUuDQaDwWAwGAy3wYJLg8FgMBgMBsNts…
题意:给出一棵字母二叉树删除叶子节点的序列,按删除的顺序排列.让你输出该棵二叉树额前序遍历的序列.思路:先把一棵树的所有删除的叶子节点序列存储下来,然后从最后一行字符串开始建树即可,最后遍历输出.    这里为方便起见,将子母转化成整数值存储. #include <iostream> #include <stdio.h> #include <string.h> #include <algorithm> /* AC 题意:给出一棵字母二叉树删除叶子节点的序列,…
题意: 递归的方式输入一个树状天平(一个天平下面挂的不一定是砝码还可能是一个子天平),判断这个天平是否能满足平衡条件,即W1 * D1 == W2 * D2. 递归的方式处理输入数据感觉很巧妙,我虽然能理解,但自己是写不出来的. 这里的参数是传引用,所以是在递归回来的时候才会赋值的. //#define LOCAL #include <iostream> using namespace std; bool solve(int& w) { int w1, d1, w2, d2; bool…
额,刘汝佳小白里面的配套题目. 题目求二叉树同垂直线上结点值的和. 可以用二叉树做,挺水的其实. 尝试使用dfs实现了:开一个大点的数组,根节点为最中间那点,然后读取时就可以进行和的计算了. 代码: #include <cstdio> #include <cstring> const int maxn = 10000; int n = 500, tmp, num = 1; int cnt[maxn] = {0}; bool input(void) { scanf("%d&…
题目描述: 原题:https://vjudge.net/problem/UVA-699 题目思路: 1.依旧二叉树的DFS 2.建树过程中开个数组统计 //紫书源代码WA AC代码: #include <iostream> #include <cstring> using namespace std; ; int sum[maxn] ; void buildtree(int val,int p) //建树,p为树根的水平位置 { int vl,vr; ) return ; sum[…
题目链接:https://vjudge.net/problem/UVA-699 题目大意:给一颗二叉树,每个结点都有一个水平位置 :左子节点在它左边的1个单位,右子结点在它右边1个单位.从左向右输出每个水平位置的所有结点的权值之和. 思路:用sum[i]表示第i个水平位置的总和  .  其实这题并不难  但是因为刚刚学数据结构  二叉树并不熟悉  所以也列出来 看代码: #include<iostream> #include<string.h> #include<stdio.…
第一次动手写二叉树的,有点小激动,64行的if花了点时间,上传leetcode一次点亮~~~ /* inorder traversal binary tree */ #include <stdio.h> #include <stdlib.h> struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; }; int* inorderTraversal(struct TreeNode* root,…