题解 | #输出二叉树的右视图#

输出二叉树的右视图

https://www.nowcoder.com/practice/c9480213597e45f4807880c763ddd5f0

#include <iostream>
#include <iterator>
#include <queue>
#include <strings.h>
#include <vector>
class Solution {
  public:
    TreeNode* constructBinTree(vector<int>& preOrder, vector<int>& inOrder) {
        if (preOrder.empty()) {
            return nullptr;
        }
        auto rootVal = preOrder[0];
        auto inOrderRootIndex = 0;
        for (int i = 0; i < inOrder.size(); i++) {
            if (inOrder[i] == rootVal) {
                inOrderRootIndex = i;
                break;
            }
        }
        auto root = new TreeNode(preOrder[0]);
        auto inOrder1 = std::vector<int>(inOrder.begin(),
                                         inOrder.begin() + inOrderRootIndex);
        auto inOrder2 = std::vector<int>(inOrder.begin() + inOrderRootIndex + 1,
                                         inOrder.end());
        auto preOrder1 = std::vector<int>(preOrder.begin() + 1,
                                          preOrder.begin() + inOrderRootIndex + 1);
        auto preOrder2 = std::vector<int>(preOrder.begin() + inOrderRootIndex + 1,
                                          preOrder.end());
        if (!inOrder1.empty()) {
            root->left = constructBinTree(preOrder1, inOrder1);
        }
        if (!inOrder2.empty()) {
            root->right = constructBinTree(preOrder2, inOrder2);
        }
        return root;
    }

    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 求二叉树的右视图
     * @param preOrder int整型vector 先序遍历
     * @param inOrder int整型vector 中序遍历
     * @return int整型vector
     */
    vector<int> solve(vector<int>& preOrder, vector<int>& inOrder) {
        // write code here
        if (preOrder.empty()) {
            return preOrder;
        }
        std::vector<int> rigthViewNodeVals;
        auto root = constructBinTree(preOrder, inOrder);
        rigthViewNodeVals.push_back(root->val);
        queue<TreeNode*> fatherNodes;
        fatherNodes.push(root);
        int recordVal = 0;
        bool flag = false;
        while (!fatherNodes.empty()) {
            auto length = fatherNodes.size();
            for (int i = 0; i < length; i++) {
                if (fatherNodes.front()->left != nullptr) {
                    flag = true;
                    fatherNodes.push(fatherNodes.front()->left);
                    recordVal = fatherNodes.front()->left->val;
                    std::cout << recordVal << " " << endl;
                }
                if (fatherNodes.front()->right != nullptr) {
                    flag = true;
                    fatherNodes.push(fatherNodes.front()->right);
                    recordVal = fatherNodes.front()->right->val;
                    std::cout << recordVal << " " << endl;
                }
                fatherNodes.pop();
            }
            std::cout << "flag: "<< flag <<endl;
            if (flag) {
                rigthViewNodeVals.push_back(recordVal);
            }
            flag = false;
        }
        return rigthViewNodeVals;
    }
};

在线编程练习 文章被收录于专栏

C++在线编程练习题解

全部评论

相关推荐

09-17 17:09
门头沟学院 Java
雨忄:有人给出过解法,拖晚点去,然后到时候再找其他理由商量,既增加他们的筛人成本,不一定会给你收回offer ,也能占位避免工贼
秋招的嫡长offer
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务