# Binary-Tree-Right-Side-View

Dec 02, 2017

Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom.

For example: Given the following binary tree,

1 <— /
2 3 <— \
5 4 <—

You should return [1, 3, 4].

vector<int> rightSideView1(TreeNode* root) {
vector<int> ret;
helper(root,0,ret);
return ret;
}
void helper(TreeNode *root,int height,vector<int> &ret) {
if (root == nullptr) return ;
if (height == ret.size()) ret.push_back(root->val);
helper(root->right,height + 1,ret);
helper(root->left,height+1,ret);
}


public List<Integer> rightSideView(TreeNode root) {
// reverse level traversal
List<Integer> result = new ArrayList();
if (root == null) return result;

queue.offer(root);
while (queue.size() != 0) {
int size = queue.size();
for (int i=0; i<size; i++) {
TreeNode cur = queue.poll();
if (cur.right != null) queue.offer(cur.right);
if (cur.left != null) queue.offer(cur.left);
}

}
return result;
}

LeetCodeLeetCodeTree

Valid-Triangle-Number

Swaps-Nodes-in-Pairs