zoukankan      html  css  js  c++  java
  • Sicily 1156. Binary tree

    题目地址:1156. Binary tree

    思路:

          如何愉快地前序输出呢,要在存储数据的时候根据位置来存放数据!

      一开始被自己蠢哭,一直以为第一个输入的就是根结点(例子的祸害呀啊啊啊!!!!),结果证明不是的,所以呢,我们要找出根结点,那么怎么找根结点呢,我用了一个向量来存储下标值,遍历向量值,把节点的左右值作为下标的那个数据设为非根结点,然后剩下的那个就是根节点了。

         具体代码如下:

     1 #include <iostream>
     2 #include <vector>
     3 using namespace std;
     4 
     5 struct Store{
     6     char node;
     7     int left;
     8     int right;
     9     bool is_root;
    10 }store[1001];
    11 
    12 void print(int x) {
    13     if (x == 0) return;
    14     cout << store[x].node;
    15     print(store[x].left);
    16     print(store[x].right);
    17 }
    18 int main() {
    19     int t;
    20     while (cin >> t) {
    21         int index;
    22         vector<int> store_index;
    23         for (int i = 0; i < t; i++) {
    24             cin >> index;
    25             cin >> store[index].node >> store[index].left
    26                 >> store[index].right;
    27             store[index].is_root = true;
    28             store_index.push_back(index);
    29         }
    30         for (int i = 0; i < t; i++) {
    31             int left = store[store_index[i]].left;
    32             int right = store[store_index[i]].right;
    33             store[left].is_root = false;
    34             store[right].is_root = false;
    35         }
    36         int root_node;
    37         for (int i = 0; i < t; i++) {
    38             if (store[store_index[i]].is_root == true) {
    39                 root_node = store_index[i];
    40                 break;
    41             }
    42         }
    43         print(root_node);
    44         cout << endl;
    45     }
    46     
    47     return 0;
    48 }
  • 相关阅读:
    ubuntu配置bonding 武汉
    桌面图标加载与存储
    launcher 图标删除分析
    Inflater
    Android屏幕元素层次结构
    简述Android触摸屏手势识别
    RelativeLayout && inflate
    manifest分析
    vacantcell缓存分析
    桌面快捷键和桌面livefolder
  • 原文地址:https://www.cnblogs.com/winray/p/4095606.html
Copyright © 2011-2022 走看看