Extremely simple iterative c++ solution


  • 0
    Y

    function view() returns the depth of the tree.

    public:
    
        vector<int> order;
        
        vector<int> rightSideView(TreeNode* root) {
       int m = view(root, 0);
    	return order;
        }
        
        int view(TreeNode* node, int n){
    		if (!node) return 0;
    
    	if (n==0) order.push_back(node->val);
    
        int m = view(node->right, max(0, n-1) );
    	return max( view(node->left, max(m,n-1 )), m) + 1;
    }

Log in to reply
 

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