Simple C++ solution using queue


  • 0
    Q
    vector<int> rightSideView(TreeNode* root) {
    	vector<int> r;
    	if (root == NULL) return r;
    	queue<TreeNode*> q;
    	q.push(root);
    	while (q.empty() == false) {
    		r.push_back(q.front()->val);
    		queue<TreeNode*> tmpQueue;
    		tmpQueue.swap(q);
    		while (tmpQueue.empty() == false) {
    			TreeNode* node = tmpQueue.front();
    			if (node->right) q.push(node->right);
    			if (node->left) q.push(node->left);
    			tmpQueue.pop();
    		}
    	}
    	return r;
    }
    

Log in to reply
 

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