8ms c++ solution


  • 0
    Z

    my 8ms c++ soluton

    	vector<int> rightSideView(TreeNode *root) 
    {
    	vector<int> ret;
    
    	queue<TreeNode *> queue_one;
    	queue<TreeNode *> queue_two;
    
    	queue<TreeNode *> *pone = &queue_one;
    	queue<TreeNode *> *ptwo = &queue_two;
    
    	if (root == 0)	return ret;
    
    	pone->push(root);
    	while (!pone->empty()){
    		TreeNode *node = pone->front();
    		pone->pop();
    
    		if (node->left)	ptwo->push(node->left);
    		if (node->right) ptwo->push(node->right);
    
    		if (pone->empty()){
    			ret.push_back(node->val);
    
    			queue<TreeNode *> *t = pone;
    			pone = ptwo;
    			ptwo = t;
    		}
    	}
    
    	return ret;
    }

Log in to reply
 

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