<ul id="wkeia"><dfn id="wkeia"></dfn></ul>
  • <fieldset id="wkeia"></fieldset>
  • <option id="wkeia"></option>
  • <ul id="wkeia"></ul>
    • 。?/a>
    • 第一次信用卡逾期三天怎么辦
      ?停息掛賬后會終生有污點嗎?
    • 營銷軟文推廣平臺|推廣宣傳這幾點你知道嗎
    • 肺炎疫情導致信用卡逾期怎么辦
      ?信用卡逾期了怎么辦理停息掛賬
    • 青島合同糾紛律師服務平臺律所-理財合同糾紛律師排行
    • 家具推廣軟文|推廣宣傳這幾點你知道嗎?
    • 信用卡逾期被通知起訴怎么辦?專門辦理停息掛賬的人可信嗎
    • 裝飾公司軟文推廣?軟文推廣宣傳一定要注意的細節(jié)
    • 福鼎白茶攤銷文案怎么寫?陜西靠譜的福鼎白茶招商
    • 買房信用卡有逾期呆賬怎么辦?專門辦理停息掛賬的人可信嗎
    • 公司軟文|新聞發(fā)稿宣傳一定要注意的細節(jié)
    • 安徽鍋爐煙囪美化施工_鍋爐技術論文(網(wǎng)站更新中)
    • 育雛專用鍋爐哪家實惠?鍋爐基礎培訓_2月推薦
    • 欠錢逾期沒歸還怎么辦信用卡?個性化分期后是不是一輩子都毀了
    • 網(wǎng)站軟文|軟文發(fā)稿到底需要發(fā)布多少家效果比較好?
    • 安徽消防設施操作員試題-初級消防設施操作員考前培訓班
    • 掛靠中級消防設施操作員-初級消防設施操作員考前培訓班
    • 推廣軟文網(wǎng)-軟文發(fā)稿網(wǎng)這幾點你知道嗎
    • 消防設施操作員上海補貼-中級消防設施操作員培訓學費多少錢
    • 消防設施操作員 掛證-初級消防設施操作員考前培訓班
    • 固定燃煤鍋爐價格多少
      ?鍋爐合資品牌_2023已更新
    • 信用卡逾期過申請房貸怎么辦
      ?信用卡申請個性化分期不給批怎么辦
    • 信用卡欠款5萬逾期了怎么辦?信用卡怎么申請個性化分期
    • 商標注冊成功-注冊商標圖
    • 活動軟文是什么-軟文發(fā)稿網(wǎng)這幾點你知道嗎
    • 江門奶茶店加盟哪個好?江門奶茶店網(wǎng)紅水果茶加盟
    • 軟文是什么有什么優(yōu)點嗎:軟文發(fā)稿網(wǎng)那家平臺比較好
    • 銀行信用卡逾期自動扣款怎么辦
      ?欠網(wǎng)貸5萬無力償還了怎么辦?
    • 進監(jiān)獄信用卡逾期怎么辦上海?網(wǎng)貸還不上會影響孩子上學嗎
    • 軟文代發(fā)找誰|軟文發(fā)稿到底需要發(fā)布多少家效果比較好?
    • 注冊商標福鼎白茶怎么申請
      ?福鼎白茶庫存量大嗎?
    • 鹽城委托合同糾紛律師-最新合同糾紛案例
    • 鍋爐蒸汽燜井采油_鍋爐循環(huán)停滯(網(wǎng)站更新中)
    • 信用卡八千額度逾期三年怎么辦
      ?信用卡3萬逾期多久會被起訴?
    • 信用卡欠幾千塊逾期怎么辦?信用卡3萬逾期多久會被起訴
    • 信用卡逾期抵押的車輛怎么辦
      ?信用卡一萬逾期多久會被起訴
    • 紅光鍋爐安全生產事故_鍋爐材料標準_2月推薦
    • 信用卡網(wǎng)貸逾期50萬怎么辦
      ?信用卡逾期起訴要多長時間?
    • 銷售數(shù)控燃煤鍋爐報價_直流鍋爐調節(jié)(網(wǎng)站更新中)
    • 信用卡逾期兩次怎么辦?信用卡逾期起訴要多長時間
    • 信用卡貸款逾期怎么辦
      ?廣州信用卡逾期多久會被起訴
    • 系列推廣軟文范例-軟文發(fā)稿網(wǎng)這幾點你知道嗎
    • 營銷軟文推廣平臺|新聞發(fā)稿宣傳一定要注意的細節(jié)
    • 大同冷凝式鍋爐圖片_鍋爐省煤器設計(網(wǎng)站推薦中)
    • 信用卡逾期5天怎么辦?信用卡逾期兩年沒還怎么辦
    •  

      當前快看:1159 Structure of a Binary Tree + 根據(jù)前序和中序構建二叉樹+ 層序遍歷模板復習

      發(fā)布時間:2023-05-03 18:24:06  |  來源:博客園  


      (資料圖片僅供參考)

      題目鏈接:https://pintia.cn/problem-sets/994805342720868352/exam/problems/1478635126488367104

      ,今天的bug出在了下面這條語句。

      if (tree[root_key].left * tree[root_key].right < 0) full_tree = false;

      我寫成了

      full_tree = !(tree[root_key].left * tree[root_key].right < 0); // 這就會導致full_tree即便變成了false也有可能變回true
      。導致錯判

      根據(jù)前序和中序構建二叉樹

      參考的柳神代碼,靈活的點就在于

      ,用下標表示數(shù)組區(qū)間
      ,嗯,相比直接傳數(shù)組的引用
      ,輕了不少

      int build(int R, int start, int end, int fa) { // 1. post數(shù)組的最右邊的位置;2. start, end : in數(shù)組的起始位置
      ;3. 對于這題來說還需要記錄父節(jié)點fa
      。 if (start > end) return -1; int root_key = post[R], pos = start; while (in[pos] != root_key && pos < end) pos++; tree[root_key].level = tree[fa].level + 1; tree[root_key].fa = fa; tree[root_key].left = build(R - 1 - (end - pos), start, pos - 1, root_key); // 記住post的最后一個元素的下標一定要用 R 來相對計算,而不是只用 pos
      ,不然在遞歸的過程中
      ,即便前幾層看不出什么,往后一定會發(fā)生錯位
      。 tree[root_key].right = build(R - 1, pos + 1, end, root_key); // 下標的選擇是經常容易出bug的
      ,一定要想清楚 if (tree[root_key].left * tree[root_key].right < 0) full_tree = false; return root_key;}

      題解代碼:

      #include#include#include#includeusing namespace std;int n, m;struct Node {    Node() {        fa = left = right = -1;    }    int level, fa, left, right;} tree[1005];bool full_tree = true;int in[35], post[35], num1, num2;int build(int R, int start, int end, int fa) { // 1. post數(shù)組的最右邊的位置;2. start, end : in數(shù)組的起始位置
      ;3. 對于這題來說還需要記錄父節(jié)點fa
      。 if (start > end) return -1; int root_key = post[R], pos = start; while (in[pos] != root_key && pos < end) pos++; tree[root_key].level = tree[fa].level + 1; tree[root_key].fa = fa; tree[root_key].left = build(R - 1 - (end - pos), start, pos - 1, root_key); // 記住post的最后一個元素的下標一定要用 R 來相對計算,而不是只用 pos
      ,不然在遞歸的過程中
      ,即便前幾層看不出什么,往后一定會發(fā)生錯位
      。 tree[root_key].right = build(R - 1, pos + 1, end, root_key); // 下標的選擇是經常容易出bug的
      ,一定要想清楚 if (tree[root_key].left * tree[root_key].right < 0) full_tree = false; return root_key;}bool siblings(int a, int b) { return tree[a].fa == tree[b].fa;}bool same_level(int a, int b) { return tree[a].level == tree[b].level;}bool parent(int a, int b) { return tree[b].fa == a;}bool left_child(int a, int b) { return tree[b].left == a;}bool right_child(int a, int b) { return tree[b].right == a;}int main() { cin >> n; for (int i = 0; i < n; i++) cin >> post[i]; for (int i = 0; i < n; i++) cin >> in[i]; int root = post[n - 1]; build(n - 1, 0, n - 1, 0); cin >> m; while (m--) { string str; cin >> str; if (str[0] == "I") { getline(cin, str); cout << (full_tree ? "Yes" : "No") << endl; } else { num1 = stoi(str); cin >> str; if (str[0] == "a") { cin >> num2 >> str >> str; if (str[0] == "s") { cout << (siblings(num1, num2) ? "Yes" : "No") << endl; } else { getline(cin, str); cout << (same_level(num1, num2) ? "Yes" : "No") << endl; } } else { cin >> str >> str; switch(str[1]) { case "o" : { cout << (root == num1 ? "Yes" : "No") << endl; } break; case "a" : { cin >> str >> num2; cout << (parent(num1, num2) ? "Yes" : "No") << endl; } break; case "e" : { cin >> str >> str >> num2; cout << (left_child(num1, num2) ? "Yes" : "No") << endl; } break; case "i" : { cin >> str >> str >> num2; cout << (right_child(num1, num2) ? "Yes" : "No") << endl; } break; } } } } return 0;}

      剛做的時候以為要用層序遍歷,順便復習一下吧

      層序遍歷模板代碼:

      vector> level_order(Node *root) {    vector> res;    queue q;    if (root) q.push(root);    while (q.size()) {        int size = q.size();        vector v;        for (int i = 0; i < size; i++) {            Node *node = q.front();            q.pop();            // 上一排的元素依次出隊            v.push_back(node->val);            // 下一排的節(jié)點全部入隊            if (node->left) q.push(node->left);            if (node->right) q.push(node->right);        }        // 存入一排        res.push_back(v);    }    return res;}

      關鍵詞:

       

      最近更新