-
Notifications
You must be signed in to change notification settings - Fork 241
/
Copy pathBinaryTreeRightSideView.cpp
68 lines (50 loc) · 1.69 KB
/
BinaryTreeRightSideView.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// https://leetcode.com/problems/binary-tree-right-side-view/description/
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void storeRightNodesValues(TreeNode* root, int level, vector<int> &ans){
if(root==NULL)
return;
if(level == ans.size())
ans.push_back(root->val);
storeRightNodesValues(root->right, level+1, ans);
storeRightNodesValues(root->left, level+1, ans);
}
vector<int> rightSideView(TreeNode* root) {
vector<int> ans;
storeRightNodesValues(root, 0, ans);
return ans;
/*
vector<int> ans;
if(root==NULL)
return ans;
map<int,int> levelMapping;
queue<pair<TreeNode*,int>> q;
q.push(make_pair(root, 0));
while(!q.empty()){
pair<TreeNode*,int> front= q.front();
q.pop();
TreeNode* currNode= front.first;
int level= front.second;
levelMapping[level]= currNode->val;
if(currNode->left)
q.push(make_pair(currNode->left, level+1));
if(currNode->right)
q.push(make_pair(currNode->right, level+1));
}
for( auto i : levelMapping)
ans.push_back(i.second);
return ans;
*/
}
};