diff --git "a/leetcode2/1easy/\354\235\264\354\244\200\354\227\264/94.cpp" "b/leetcode2/1easy/\354\235\264\354\244\200\354\227\264/94.cpp" new file mode 100644 index 00000000..03542f91 --- /dev/null +++ "b/leetcode2/1easy/\354\235\264\354\244\200\354\227\264/94.cpp" @@ -0,0 +1,36 @@ +/** + * 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: + vector inorderTraversal(TreeNode* root) { + vector output; + stack s; + TreeNode *cur = root; + + while (cur || !s.empty()) + { + while (cur) + { + s.push(cur); + cur = cur->left; + } + + cur = s.top(); + s.pop(); + + output.push_back(cur->val); + + cur = cur->right; + } + return output; + } +}; \ No newline at end of file diff --git "a/leetcode2/2medium/\354\235\264\354\244\200\354\227\264/200.cpp" "b/leetcode2/2medium/\354\235\264\354\244\200\354\227\264/200.cpp" new file mode 100644 index 00000000..b0ebd4fc --- /dev/null +++ "b/leetcode2/2medium/\354\235\264\354\244\200\354\227\264/200.cpp" @@ -0,0 +1,49 @@ +class Solution { +public: + int dx[4] = {1, 0, -1, 0}; + int dy[4] = {0, 1, 0, -1}; + bool visited[300][300] = {}; + + void bfs(vector>& grid, int y, int x) + { + queue> q; + visited[y][x] = true; + q.push(make_pair(y,x)); + + while (!q.empty()) + { + pair p = q.front(); + q.pop(); + + for (int i = 0; i < 4; i++) + { + int tmpY = p.first + dy[i]; + int tmpX = p.second + dx[i]; + if (tmpY < 0 || tmpY >= grid.size() || tmpX < 0 || tmpX >= grid[0].size()) + continue; + if (grid[tmpY][tmpX] == '1' && visited[tmpY][tmpX] == false) + { + q.push(make_pair(tmpY, tmpX)); + visited[tmpY][tmpX] = true; + } + } + } + } + + + int numIslands(vector>& grid) { + int output = 0; + for (int i = 0; i < grid.size(); i++) + { + for (int j = 0; j < grid[0].size(); j++) + { + if (grid[i][j] == '1' && visited[i][j] == false) + { + output++; + bfs(grid, i, j); + } + } + } + return output; + } +}; \ No newline at end of file