c_cpp 199.cpp

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp 199.cpp相关的知识,希望对你有一定的参考价值。

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {
        vector<int> result;
        if (root == NULL) return result;
        int depth = getDepth(root);
        vector<bool> s;
        for(int i = 0; i < depth; i++){
            s.push_back(false);
        }
        cout << s.size() << endl;
        int floor = 0;
        traverse(root, result, s, floor);
        return result;
    }
    
    void traverse(TreeNode *root,vector<int> &result,vector<bool> &sign,int floor){
        if (root == NULL) return;
        if (sign[floor] == false) {
            result.push_back(root->val);
            sign[floor] = true;
        }
        traverse(root->right, result, sign, floor+1);
        traverse(root->left, result, sign, floor+1);
    }
    
    int getDepth(TreeNode* root) {
        if (root == NULL) return 0;
        int l = getDepth(root->left);
        int r = getDepth(root->right);
        return (r > l ? r : l) + 1;
    }
};

以上是关于c_cpp 199.cpp的主要内容,如果未能解决你的问题,请参考以下文章

c_cpp 200.岛屿数量

c_cpp 127.单词阶梯

c_cpp MOFSET

c_cpp MOFSET

c_cpp 31.下一个排列

c_cpp string→char *