Created
August 3, 2020 03:38
-
-
Save eklitzke/ce4fc4e88400f8928821ddbb84f45009 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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 { | |
map<int, vector<int>> levels; | |
public: | |
void helper(const TreeNode *node, int height) { | |
if (node == nullptr) { | |
return; | |
} | |
levels[height].push_back(node->val); | |
helper(node->left, height + 1); | |
helper(node->right, height + 1); | |
} | |
vector<int> rightSideView(const TreeNode* root) { | |
helper(root, 0); | |
vector<int> ans; | |
ans.reserve(levels.size()); | |
for (const auto &[_, row] : levels) { | |
ans.push_back(row.back()); | |
} | |
return ans; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment