Leetcode 199 Solution
This article provides solution to leetcode question 199 (binary-tree-right-side-view)
Access this page by simply typing in "lcs 199" in your browser address bar if you have bunnylol configured.
Leetcode Question Link
https://leetcode.com/problems/binary-tree-right-side-view
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 {
vector<int> m_res;
public:
void visit(TreeNode* root, int layer) {
if (root == NULL)
return;
if (m_res.size() < layer + 1)
m_res.resize(layer + 1);
m_res[layer] = root->val;
visit(root->left, layer + 1);
visit(root->right, layer + 1);
}
vector<int> rightSideView(TreeNode* root) {
visit(root, 0);
return m_res;
}
};