Clean c++ passed code using iterator


  • 0
    L
    vector<int> rightSideView(TreeNode *root) {
        vector<int> vi;
        if (!root) return vi;
        queue<TreeNode *> q;
        q.push(root);
        while(!q.empty()) {
            int size = q.size();
            for (int i=0; i<size; i++) {
                TreeNode *node = q.front();
                q.pop();
                if (i==(size-1)) vi.push_back(node->val);
                if (node->left) q.push(node->left);
                if (node->right) q.push(node->right);
            }
        }
        return vi;
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.