4ms, Easy-understanding c++ solution


  • 0
    C
    //Binary Tree Right Side View
    class Solution {
    public:
    	vector<int> rightSideView(TreeNode* root) {
    	
    		if (!root)
    			return vc;
    	
    		helper(root, 0);
    		return vc;
    	}
    private:
    	vector<int> vc;
    	unordered_map<int, bool> um;
    	void helper(TreeNode* root, int lev)
    	{
    		if (!root)
    			return;
    		if (!um[lev])
    		{
    			vc.push_back(root->val);
    			um[lev] = true;
    		}
    		helper(root->right, lev + 1);
    		helper(root->left, lev + 1);
    	}
    };

Log in to reply
 

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