My 4-ms c++ solution


  • 0
    J
    class Solution {
    public:
    vector<int> rightSideView(TreeNode* root)
    {
            vector<int> result;
            
            if (root == NULL)
            {
                return result;
            }
            
            vector<TreeNode*> nodes;
            nodes.push_back(root);
            while(nodes.size() > 0)
            {
                result.push_back(nodes[0]->val);
                
                vector<TreeNode*> tmp(nodes);
                nodes.clear();
                
                for(int i = 0; i < tmp.size(); i++)
                {
                    TreeNode* node = tmp[i];
                    
                    if (node->right != NULL)
                    {
                        nodes.push_back(node->right);
                    }
                    
                    if (node->left != NULL)
                    {
                        nodes.push_back(node->left);
                    }
                }
            }
            
            return result;
        }
    };

Log in to reply
 

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