void rightView(TreeNode* cur, int curDepth, vector<int> &result){ if(!cur){return;} if(result.size() < curDepth){ result.push_back(cur -> val); } //always along the level's right most node rightView(cur -> right, curDepth + 1, result); rightView(cur -> left, curDepth + 1, result); }
// Driver Program to test above functions int main() { struct Node *root = newNode(1); root->left = newNode(2); root->right = newNode(3); root->left->left = newNode(4); root->left->right = newNode(5); root->right->left = newNode(6); root->right->right = newNode(7); root->right->left->right = newNode(8); rightView(root); return 0; }
vector<int> rightSideView(TreeNode* root) { vector<int> result; rightView(root, 1, result); return result; }