rocksc30 / LeetCode

用于力扣刷题打卡
2 stars 0 forks source link

199. 二叉树的右视图 #37

Open Ni-Guvara opened 1 year ago

Ni-Guvara commented 1 year ago
class Solution {
public:
    vector<int> rightSideView(TreeNode* root) {

            vector<int> ret;

            queue<TreeNode *> aux;

            if(root)  aux.push(root);

            while(!aux.empty())
            {
                int len = aux.size();

                ret.push_back(aux.back()->val);

                for(int idx = 0; idx < len; idx++ )
                {
                    TreeNode * node = aux.front();
                    aux.pop();

                    if(node->left)
                        aux.push(node->left);
                    if(node->right)
                        aux.push(node->right);

                }
            } 

            return ret;
    }
};