File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -42,6 +42,9 @@ Explanation:
4242
4343## 代码
4444
45+ * 语言支持:JS,C++
46+
47+ Javascript Code:
4548``` js
4649/*
4750 * @lc app=leetcode id=199 lang=javascript
@@ -115,6 +118,38 @@ var rightSideView = function(root) {
115118 return ret;
116119};
117120```
121+ C++ Code:
122+ ``` C++
123+ /* *
124+ * Definition for a binary tree node.
125+ * struct TreeNode {
126+ * int val;
127+ * TreeNode *left;
128+ * TreeNode *right;
129+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
130+ * };
131+ */
132+ class Solution {
133+ public:
134+ vector<int > rightSideView(TreeNode* root) {
135+ auto ret = vector<int >();
136+ if (root == nullptr) return ret;
137+ auto q = queue<const TreeNode* >();
138+ q.push(root);
139+ while (!q.empty()) {
140+ auto sz = q.size();
141+ for (auto i = 0; i < sz; ++i) {
142+ auto n = q.front();
143+ q.pop();
144+ if (n->left != nullptr ) q.push(n->left);
145+ if (n->right != nullptr ) q.push(n->right);
146+ if (i == sz - 1) ret.push_back(n->val);
147+ }
148+ }
149+ return ret;
150+ }
151+ };
152+ ```
118153
119154## 扩展
120155
You can’t perform that action at this time.
0 commit comments