Right Pre-order Traversal using C++


  • 0
    class Solution {
    public:
    
        void rightPreOrder(TreeNode* node, int level, vector<int>& res) {
            if(node==NULL)
                return;
            if(res.size()==level)
                res.push_back(node->val);
            rightPreOrder(node->right, level+1, res);
            rightPreOrder(node->left, level+1, res);
        }
        
        vector<int> rightSideView(TreeNode* root) {
            vector<int> res;
            rightPreOrder(root,0,res);
            return res;
        }
    };

Log in to reply
 

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