zoukankan      html  css  js  c++  java
  • J 判断二叉树每个结点的权值是否关于根节点完全对称

    如果二叉树每个结点的权值关于根节点完全对称 就输出Yes

    Sample Input

    2
    7 //结点
    1 2 3 //结点1的左孩子是结点2 右孩子是结点3
    2 4 5
    3 6 7
    4 0 0
    5 0 0
    6 0 0
    7 0 0
    1 2 2 3 4 4 3 //权值
    5
    1 2 3
    2 0 4
    3 0 5
    4 0 0
    5 0 0
    1 2 2 3 3

    Sample Output

    Yes
    No

     1 # include <cstdio>
     2 # include <cstring>
     3 # define LL long long
     4 using namespace std ;
     5 
     6 bool flag  ;
     7 
     8 struct tree
     9 {
    10     int lson ;
    11     int rson ;
    12     int num ;
    13     int id ;
    14 }t[120];
    15 
    16 void dfs(int r1 , int r2)
    17 {
    18     if (r1 == 0 && r2 == 0)
    19         return ;
    20     if (r1 == 0 && r2 != 0 || r1 != 0 && r2 == 0 || t[r1].num != t[r2].num)
    21     {
    22         flag = 0 ;
    23         return ;
    24     }
    25     if (!flag)
    26         return ;
    27     dfs(t[r1].lson , t[r2].rson) ;
    28     dfs(t[r1].rson , t[r2].lson) ;
    29 }
    30 
    31 int main ()
    32 {
    33    // freopen("in.txt","r",stdin) ;
    34     int T ;
    35     scanf("%d" , &T) ;
    36     while (T--)
    37     {
    38         int n ;
    39         scanf("%d" , &n) ;
    40         int i ;
    41         int a ,b ,c ;
    42         for (i = 1 ; i <= n ;i++)
    43         {
    44             scanf("%d %d %d" , &a , &b , &c) ;
    45             t[a].lson = b ;
    46             t[a].rson = c ;
    47         }
    48         for (i = 1 ; i <= n ;i++)
    49             scanf("%d" , &t[i].num) ;
    50         flag = 1 ;
    51 
    52         dfs(t[1].lson , t[1].rson) ;
    53         if (flag)
    54             printf("Yes
    ") ;
    55         else
    56             printf("No
    ") ;
    57     }
    58 
    59     return 0 ;
    60 }
    View Code
  • 相关阅读:
    BestCoder Round #61 (div.2)
    CCPC L(水)
    CCPC A(模拟)
    暗网是什么?如何进入暗网?
    社会工程学:关于一些信息收集的网站
    Flask开发系列之Web表单
    Flask开发系列之模板
    [转]Python 资源大全中文版
    python字符串/列表/字典互相转换
    Flask开发系列之Flask+redis实现IP代理池
  • 原文地址:https://www.cnblogs.com/mengchunchen/p/4528290.html
Copyright © 2011-2022 走看看