華為9.8筆試題C++


問題

給出一顆二叉樹,每個節點有一個編號和一個值,該值可能為負數,請你找出一個最優節點(除根節點外),使得在該節點將樹分成兩棵樹后(原來的樹移除這個節點及其子節點,新的樹以該節點為根節點),分成的兩棵樹各 節點的和之間的差絕對值最大。請輸出該節點編號,如有多個相同的差,輸出編號最小的節點。

輸入

4
4 9 -7 -8
0 1
0 3
1 2
第一行,四個節點,編號0-3,范圍為1-10000
第二行,節點0-3的權值
第三行到第五行,表示二叉樹各節點間的父子關系
0 1 // 節點0的左節點是1
0 3 // 節點0的右節點是3
1 2 // 節點1的左節點是2
注意:左節點永遠出現在右節點之前
0:4
/ \
1:9 3:-8
/
2:-7

輸出

節點編號,示例中編號為3的節點是最優節點

解法

(我沒有參與考試。。。我自己找的數據都可以過,邏輯應該是對的)

#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <unordered_map>

using namespace std;
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    explicit TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

TreeNode *creatTree(vector<int> &valArr, vector<vector<int>> &arr){
    unordered_map<int, TreeNode*> map;
    for(auto &it:arr){
        if(map.find(it[0]) == map.end()){
            auto node = new TreeNode(it[0]);
            map[it[0]] = node;
        }
        auto node = map[it[0]];
        if(node->left != nullptr){
            node->right = new TreeNode(it[1]);
            map[it[1]] = node->right;
        }else{
            node->left = new TreeNode(it[1]);
            map[it[1]] = node->left;
        }
    }
    return map[arr[0][0]];
}
unordered_map<int, int> hashMap;
int proOrderByRecur(TreeNode *node, vector<int> &valArr){
    if(node == nullptr){
        return 0;
    }
    int L = proOrderByRecur(node->left, valArr);
    int R = proOrderByRecur(node->right, valArr);
    hashMap[node->val] = L + R + valArr[node->val];
    return L + R + valArr[node->val];
}
void solution(TreeNode *head, vector<int> &valArr) {
    proOrderByRecur(head, valArr);
    int index ;
    int total = hashMap[head->val];
    int maxVal = 0;

    for(auto &it : hashMap){
        if(it.first == head->val) continue;
        if(abs(total - 2 * it.second) > maxVal){
            maxVal = abs(total - 2 * it.second);
            index = it.first;
        }else if(abs(total - 2 * it.second) == maxVal){
            index = min(index, it.first);
        }
    }
    cout<<index<<endl;
}
void test(){
    int n;
    while(cin >> n){
        cin.ignore();
        string s1;
        while(getline(cin, s1)){
            stringstream ss1(s1);
            vector<int> valArr;
            while(getline(ss1, s1, ' ')){
                valArr.push_back(stoi(s1));
            }
            vector<vector<int>> arr;

            for (int i = 0; i < n-1; ++i) {
                string s2;
                while(getline(cin, s2)){
                    stringstream ss2(s2);
                    vector<int> A;
                    while(getline(ss2, s2, ' ')){
                        A.push_back(stoi(s2));
                    }
                    arr.push_back(A);
                    break;
                }
            }
            auto head = creatTree(valArr, arr);
            solution(head, valArr);
            break;
        }
    }
}

int main() {
    test();
    return 0;
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM