- 二叉树的右视图
/**
* 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 {
private:
vector<int>res;
public:
void dfs(TreeNode * root,int depth){
if(root==NULL) return ;
if(res.size()==depth) res.push_back(root->val);
dfs(root->right,depth+1);
dfs(root->left,depth+1);
}
vector<int> rightSideView(TreeNode* root) {
dfs(root,0);
return res;
}
};