• [jobdu]二叉树的镜像


    树的镜像,这里的做法就是先序遍历的反过来呗。

    #include <iostream>
    #include <vector>
    using namespace std;
    
    void preOrder(vector<vector<int> > &tree, vector<int> &val, vector<int> &ans, int root) {
        if (root == -1) return;
        else {
            ans.push_back(val[root]);
            preOrder(tree, val, ans, tree[root][1]);
            preOrder(tree, val, ans, tree[root][0]);
        }
    }
    
    int main() {
        int n;
        while (cin >> n)
        {
            vector<vector<int> > tree(n+1);
            vector<int> val(n+1);
            for (int i = 1; i <= n; i++) {
                int tmp;
                cin >> tmp;
                val[i] = tmp;
            }
            for (int i = 1; i <= n; i++) {
                char type;
                cin >> type;
                if (type == 'd') {
                    int x, y;
                    cin >> x >> y;
                    tree[i].push_back(x);
                    tree[i].push_back(y);
                }
                else if (type == 'l') {
                    int x;
                    cin >> x;
                    tree[i].push_back(x);
                    tree[i].push_back(-1);
                }
                else if (type == 'r') {
                    int x;
                    cin >> x;
                    tree[i].push_back(-1);
                    tree[i].push_back(x);
                }
                else if (type == 'z') {
                    tree[i].push_back(-1);
                    tree[i].push_back(-1);
                }
            }
            if (n == 0) {
                cout << "NULL" << endl;
                continue;
            }
            vector<int> ans;
            preOrder(tree, val, ans, 1);
            for (int i = 0; i < ans.size() - 1; i++) {
                cout << ans[i] << " ";
            }
            cout << ans[ans.size()-1] << endl;
        }
    }
    

      

  • 相关阅读:
    ansible——playbook conditions条件判断
    ansible——playbook lookups从插件加载变量
    ansible——playbook循环
    lombok注解
    集合与集合取笛卡尔积
    List排列组合
    synchronized初识
    java IO与NIO
    文件I/O和标准I/O
    双数据源配置
  • 原文地址:https://www.cnblogs.com/lautsie/p/3422352.html
Copyright © 2020-2023  润新知