文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
2. Solution
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<int> rightSideView(TreeNode* root) {
vector<int> result;
traverseRight(root, result, 0);
return result;
}
private:
void traverseRight(TreeNode* root, vector<int>& result, int depth) {
if(!root) {
return;
}
depth++;
if(depth > result.size()) {
result.push_back(root->val);
}
traverseRight(root->right, result, depth);
traverseRight(root->left, result, depth);
}
};