My C++ Solution, postorder traversal.


  • 0
    W
    class Solution {
    public:
        vector<int> rightSideView(TreeNode* root) {
    	if (root == nullptr)
    		return vector<int>();
    	vector<int> left = rightSideView(root->left);
    	vector<int> right = rightSideView(root->right);
    	
    	if (left.size() >= right.size())
    	{
    		vector<int> res(left.size() + 1);
    		res[0] = root->val;
    		std::copy(right.begin(), right.end(), left.begin());
    		std::copy(left.begin(), left.end(), res.begin()+1);
    		return res;
    	}
    	vector<int> res(right.size()+1);
    	res[0] = root->val;
    	std::copy(right.begin(), right.end(), res.begin()+1);
    	return res;
        }
    };
    

Log in to reply
 

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