Leetcode 199. Binary Tree Right
2018-09-20 本文已影响5人
SnailTyan
文章作者:Tyan
博客:noahsnail.com | CSDN | 简书
1. Description
Binary Tree Right Side View2. 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);
}
};