Uva 839天平(二叉树dfs, 递归建树)

题意:

给定一个天平长度 输入格式为

wl dl wr dr

分别代表天平左边长度,左边重量, 右边长度, 右边重量。

如果重量为0, 说明下面还有一个天平, 递归给出。

样例输入:
1
0 2 0 4
0 3 0 1
1 1 1 1
2 4 4 2
1 6 3 2

如果天平是左右重量相等的 输出YES 否则输出NO。

分析:

既然以递归的定义输入, 那么我们程序最好写成递归建树,如果有重量是0, 那么就递归建左子树或者右子树 , 如果是叶子节点的父节点(这个天平下面没天平), 那么就判断左右是否相等返回即可(也可建一个全局变量, 一个不满足就可以输出NO了)。 我的程序是把树建造出来再判断, 有点冗余。

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6;
struct Node{
  int wl,dl,wr,dr;
  int left, right;
  Node():wl(0),dl(0),wr(0),dr(0){}
};
Node tree[maxn];
const int root = 1;
int cnt = 0, ok;
int build(int u){
    int wl,dl,dr,wr;
    scanf("%d %d %d %d",&wl,&dl,&wr,&dr);
    tree[u].wl = wl;
    tree[u].dl = dl;
    tree[u].wr = wr;
    tree[u].dr = dr;
    if(!wl) {
            tree[u].left = ++cnt;
            build(cnt);
    }
    if(!wr) {
            tree[u].right = ++cnt;
            build(cnt);
    }
}
int post(int u){
    if(!tree[u].wl) {
        tree[u].wl = post(tree[u].left);
    }
    if(!tree[u].wr) {
        tree[u].wr = post(tree[u].right);
    }
    if(tree[u].wl * tree[u].dl != tree[u].wr * tree[u].dr)
         ok = 0;
    return tree[u].wl + tree[u].wr;
}
int main(){

    int T;
    scanf("%d", &T);
    int flag = 0;
    while(T--){
        if(!flag){
            flag = 1;
        }
        else printf("\n");
        cnt = root;
        build(root);
        ok = 1;
        post(root);
        printf("%s\n",ok ? "YES":"NO");
    }
    return 0;
}
时间: 2024-08-01 08:12:04

Uva 839天平(二叉树dfs, 递归建树)的相关文章

UVA 839 Not so Mobile (递归建树 判断)

#include<cstdio> #include<iostream> #include<queue> #include<cstring> #include<string> #include<math.h> #include<stack> #include<cstdlib> #include<algorithm> #include<cctype> #include<sstream&

UVa 839 天平

原题链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=780 先建立二叉树,之后遍历. 1 #include<iostream> 2 using namespace std; 3 4 struct Node 5 { 6 int W1, D1, W2, D2; 7 Node *left; 8 Node *right; 9 };

UVa 699 The Falling Leaves(递归建树)

题意  假设一棵二叉树也会落叶  而且叶子只会垂直下落   每个节点保存的值为那个节点上的叶子数   求所有叶子全部下落后   地面从左到右每堆有多少片叶子 和上一题有点像  都是递归输入的  一个节点(设水平位置为p)  则它的左右儿子节点的水平位置分别为  p-1  p+1   也是可以边输入边处理的  输入完也就得到答案了   注意每个样例后面都有一个空行  包括最后一个 #include<cstdio> #include<cstring> using namespace s

UVA 699(二叉树建树与遍历)

M - The Falling Leaves Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-02-08) Description  The Falling Leaves  Each year, fall in the North Central region is accompanied

UVa 548 (二叉树的递归遍历) Tree

题意: 给出一棵由中序遍历和后序遍历确定的点带权的二叉树.然后找出一个根节点到叶子节点权值之和最小(如果相等选叶子节点权值最小的),输出最佳方案的叶子节点的权值. 二叉树有三种递归的遍历方式: 先序遍历,先父节点  然后左孩子  最后右孩子 中序遍历,先左孩子  然后父节点  最后父节点 后序遍历,先左孩子  然后右孩子  最后父节点 这里有更详细的解释: http://blog.csdn.net/sicofield/article/details/9066987 紫书上面写错了,后序遍历最后一

UVA 548(二叉树重建与遍历)

J - Tree Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Appoint description:  System Crawler  (2014-05-16) Description  Tree  You are to determine the value of the leaf node in a given binary tree that is the ter

[2016-02-09][UVA][839][Not so Mobile]

时间:2016-02-09 12:10:40 星期二 题目编号:UVA 839 题目大意:给定一个秤,已知秤两边的重量和到支点的距离,问秤能否平衡 分析:每个秤都是一个树的结构,只需要遍历一遍,看每个分支点能否平衡即可 方法:根绝输入,dfs下去,判断每个节点是否平衡即可 解题过程遇到问题: 刚开始,不平衡,返回值设置成0,没考虑为0的情况,不平衡的时候 改成返回-1就AC了. 刚开始忘记输出每组数据之间的空行 这里写法把整棵树都遍历了一遍,事实上,当出现不平衡的点,读取完数据之后,就可以直接返

UVa 572 Oil Deposits(DFS)

 Oil Deposits  The GeoSurvComp geologic survey company is responsible for detecting underground oil deposits. GeoSurvComp works with one large rectangular region of land at a time, and creates a grid that divides the land into numerous square plots.

二叉树遍历递归与非递归实现

说明:本文仅供学习交流,转载请标明出处,欢迎转载! 二叉树遍历是二叉树中非常基础的部分,也是学习二叉树必须熟练掌握的部分,下面我们先给出二叉树三种遍历方式的定义,并通过举例来说明二叉树遍历的过程. 二叉树的遍历分为:前序遍历(也叫先序遍历).中序遍历.后序遍历.所谓前.中.后都是根据当前子树根结点相对左右孩子的位置而言,也就是说: 前序遍历:根结点在前,即:根 ----->左------->右: 中序遍历:根结点在中间,即:左------>根------>右: 后序遍历:根结点在最