• 二叉树前中后序遍历cpp


    title

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    
    /*
    前序 HGEDBFCA, 中序 EGBD H FAC, 后序 EBDGACFH,
     root : H
     left : EGBD
     root : FAC
    */
    void reqBackOrder(string middle, string front) {
      if (middle.size() > 0) {
        char root = front[0];
        int k = middle.find(root);
        reqBackOrder(middle.substr(0, k), front.substr(1, k)); // left
        reqBackOrder(middle.substr(k + 1), front.substr(k + 1, middle.size() - k - 1));
        cout << root;
      }
    }
    
    void reqFrontOrder(string middle, string after) {
      if (middle.size() > 0) {
        char root = after[after.size() - 1];
        cout << root;
        int k = middle.find(root);
        reqFrontOrder(middle.substr(0, k), after.substr(0, k));
        reqFrontOrder(middle.substr(k + 1), after.substr(k, middle.size() - k - 1));
      }
    }
    
    int main(int argc, char *argv[]) {
      char flag;
      if (argc != 2) {
        cout << "need arg: 0 or 1 indicate front or back" << endl;
        return 0;
      }
      flag = argv[1][0];
    
      string middle;
      if ('0' == flag) {
        string front;
        cout << "input front order string" << endl;
        cin >> front;
        cout << "input middle order string" << endl;
        cin >> middle;
    
        cout << "back order string:";
        reqBackOrder(middle, front);
      } else {
        string back;
        cout << "input middle order string" << endl;
        cin >> middle;
        cout << "input middle back string" << endl;
        cin >> back;
        cout << "front order string:";
        reqFrontOrder(middle, back);
      }
    
      cout << endl;
      return 0;
    }
    
    
    
    
  • 相关阅读:
    MVC基础
    JQuery基本知识、选择器、事件、DOM操作、动画
    LinQ各种方式查询、组合查询、IQueryable集合类型
    LinQ 创建连接、简单增删改查
    webform-AJAX
    JavaScricp(总回顾)
    响应式布局(收藏)
    webform:分页组合查询
    webform:图片水印、验证码制作
    【转】开发人员一定要加入收藏夹的网站
  • 原文地址:https://www.cnblogs.com/ims-/p/13935610.html
Copyright © 2020-2023  润新知