My 4ms C++ iteratvie solution


  • 6
    E

    My c++ solution is based on level order traversal. The run time is 4ms.

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

Log in to reply
 

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