Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ซับทรีเล็กที่สุดที่มีโหนดลึกที่สุดทั้งหมดใน C++


สมมติว่าเรามีต้นไม้แบบทวิภาครากที่ root ความลึกของโหนดคือระยะทางสั้นที่สุดไปราก โหนดลึกที่สุด (deepest) คือโหนดที่มีความลึกมากที่สุดในต้นไม้ทั้งต้น ซับทรีของโหนดคือโหนดนั้นรวมกับลูกหลานทั้งหมด ต้องหาโหนดที่มีความลึกมากที่สุดที่ซับทรีของมันมีโหนดลึกที่สุดทั้งหมด

ตัวอย่าง ต้นไม้ดังภาพ ซับทรีลึกที่สุดจะเป็นดังภาพที่สอง

ซับทรีเล็กที่สุดที่มีโหนดลึกที่สุดทั้งหมดใน C++ ซับทรีเล็กที่สุดที่มีโหนดลึกที่สุดทั้งหมดใน C++

ขั้นตอนการแก้ปัญหา (DFS Post-order)

  • เมธอด solve(root) คืนคู่ (TreeNode*, depth):
    • ถ้า root เป็น null คืน (null, 0)
    • l := solve(left), r := solve(right)
    • ถ้า l.depth > r.depth คืน (l.node, l.depth + 1)
    • ถ้า l.depth < r.depth คืน (r.node, r.depth + 1)
    • คืน (root, l.depth + 1) (ความลึกเท่ากัน รากคือ LCA)
  • เมธอดหลัก เรียก solve(root).first

ตัวอย่างโค้ด (C++)

#include <bits/stdc++.h> 
using namespace std; 

class TreeNode {
public:
    int val;
    TreeNode *left, *right;
    TreeNode(int data) {
        val = data;
        left = right = NULL;
    }
};

void insert(TreeNode **root, int val) {
    queue<TreeNode*> q;
    q.push(*root);
    while(q.size()) {
        TreeNode *temp = q.front();
        q.pop();
        if(!temp->left) {
            if(val != NULL)
                temp->left = new TreeNode(val);
            else
                temp->left = new TreeNode(0);
            return;
        } else {
            q.push(temp->left);
        }
        if(!temp->right) {
            if(val != NULL)
                temp->right = new TreeNode(val);
            else
                temp->right = new TreeNode(0);
            return;
        } else {
            q.push(temp->right);
        }
    }
}

TreeNode *make_tree(vector<int> v) {
    TreeNode *root = new TreeNode(v[0]);
    for(int i = 1; i < v.size(); i++) {
        insert(&root, v[i]);
    }
    return root;
}

void tree_level_trav(TreeNode* root) {
    if (root == NULL) return;
    cout << "[";
    queue<TreeNode *> q;
    TreeNode *curr;
    q.push(root);
    q.push(NULL);
    while (q.size() > 1) {
        curr = q.front();
        q.pop();
        if (curr == NULL) {
            q.push(NULL);
        } else {
            if(curr->left) q.push(curr->left);
            if(curr->right) q.push(curr->right);
            if(curr->val == 0 || curr == NULL) {
                cout << "null" << ", ";
            } else {
                cout << curr->val << ", ";
            }
        }
    }
    cout << "]" << endl;
}

class Solution {
public:
    pair <TreeNode*, int> solve(TreeNode* root) {
        if(!root || root->val == 0) return {NULL, 0};
        pair <TreeNode*, int> L = solve(root->left);
        pair <TreeNode*, int> R = solve(root->right);
        if(L.second > R.second) return {L.first, L.second + 1};
        else if(L.second < R.second) return {R.first, R.second + 1};
        return {root, L.second + 1};
    }
    TreeNode* subtreeWithAllDeepest(TreeNode* root) {
        return solve(root).first;
    }
};

int main() {
    vector<int> v = {3,5,1,6,2,0,8,NULL,NULL,7,4};
    TreeNode *root = make_tree(v);
    Solution ob;
    tree_level_trav(ob.subtreeWithAllDeepest(root));
}

อินพุต

{3,5,1,6,2,0,8,NULL,NULL,7,4}

เอาต์พุต

[2,7,4]