File tree Expand file tree Collapse file tree 2 files changed +59
-0
lines changed
199_BinaryTreeRightSideView Expand file tree Collapse file tree 2 files changed +59
-0
lines changed Original file line number Diff line number Diff line change
1
+ /* *
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+ class Solution {
11
+ public:
12
+ vector<int > rightSideView (TreeNode* root) {
13
+ queue<TreeNode*> q[2 ];
14
+ vector<int > res;
15
+ if (!root) return res;
16
+
17
+ int l = 0 ;
18
+ q[0 ].push (root);
19
+ while (!q[0 ].empty () || !q[1 ].empty ()) {
20
+ while (!q[l].empty ()) {
21
+ TreeNode *temp = q[l].front ();
22
+ q[l].pop ();
23
+ if (temp->left ) q[1 -l].push (temp->left );
24
+ if (temp->right ) q[1 -l].push (temp->right );
25
+ if (q[l].empty ()) res.push_back (temp->val );
26
+ }
27
+ l = 1 -l;
28
+ }
29
+ return res;
30
+ }
31
+ };
Original file line number Diff line number Diff line change
1
+ class Solution {
2
+ public:
3
+ int numIslands (vector<vector<char >>& grid) {
4
+ int ans = 0 ;
5
+ int m = grid.size ();
6
+ if (m == 0 ) return ans;
7
+ int n = grid[0 ].size ();
8
+ function<void (int ,int )> change = [&](int i, int j)-> void {
9
+ if (i >= 0 && i < m && j >=0 && j < n && grid[i][j] == ' 1' ) {
10
+ grid[i][j] = ' 0' ;
11
+ change (i-1 ,j);
12
+ change (i+1 ,j);
13
+ change (i,j-1 );
14
+ change (i,j+1 );
15
+ }
16
+ };
17
+ for (int i = 0 ; i < m; ++i) {
18
+ for (int j = 0 ; j < n; ++j) {
19
+ if (grid[i][j] == ' 1' ) {
20
+ ++ans;
21
+ change (i, j);
22
+ }
23
+ }
24
+ }
25
+ return ans;
26
+ }
27
+ };
28
+
You can’t perform that action at this time.
0 commit comments