Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: update solutions to lc problems: No.1602~1603 #2477

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@

### 方法一:BFS

BFS 层序遍历,找到 $u$ 所在层的右侧相邻节点
我们可以使用广度优先搜索,从根节点开始搜索,当搜索到节点 $u$ 时,返回队列中的下一个节点

时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。

Expand Down Expand Up @@ -147,9 +147,15 @@ public:
for (int i = q.size(); i; --i) {
root = q.front();
q.pop();
if (root == u) return i > 1 ? q.front() : nullptr;
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
if (root == u) {
return i > 1 ? q.front() : nullptr;
}
if (root->left) {
q.push(root->left);
}
if (root->right) {
q.push(root->right);
}
}
}
return nullptr;
Expand Down Expand Up @@ -321,29 +327,27 @@ class Solution {
*/
class Solution {
public:
TreeNode* u;
TreeNode* ans;
int d = 0;

TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) {
this->u = u;
TreeNode* ans;
int d = 0;
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
if (!root || ans) {
return;
}
if (d == i) {
ans = root;
return;
}
if (root == u) {
d = i;
return;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
};
dfs(root, 1);
return ans;
}

void dfs(TreeNode* root, int i) {
if (!root || ans) return;
if (d == i) {
ans = root;
return;
}
if (root == u) {
d = i;
return;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
}
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@

## Solutions

### Solution 1
### Solution 1: BFS

We can use Breadth-First Search, starting from the root node. When we reach node $u$, we return the next node in the queue.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.

<!-- tabs:start -->

Expand Down Expand Up @@ -121,9 +125,15 @@ public:
for (int i = q.size(); i; --i) {
root = q.front();
q.pop();
if (root == u) return i > 1 ? q.front() : nullptr;
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
if (root == u) {
return i > 1 ? q.front() : nullptr;
}
if (root->left) {
q.push(root->left);
}
if (root->right) {
q.push(root->right);
}
}
}
return nullptr;
Expand Down Expand Up @@ -200,7 +210,11 @@ var findNearestRightNode = function (root, u) {

<!-- tabs:end -->

### Solution 2
### Solution 2: DFS

DFS performs a pre-order traversal of the binary tree. The first time we search to node $u$, we mark the current depth $d$. The next time we encounter a node at the same level, it is the target node.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.

<!-- tabs:start -->

Expand Down Expand Up @@ -291,29 +305,27 @@ class Solution {
*/
class Solution {
public:
TreeNode* u;
TreeNode* ans;
int d = 0;

TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) {
this->u = u;
TreeNode* ans;
int d = 0;
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
if (!root || ans) {
return;
}
if (d == i) {
ans = root;
return;
}
if (root == u) {
d = i;
return;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
};
dfs(root, 1);
return ans;
}

void dfs(TreeNode* root, int i) {
if (!root || ans) return;
if (d == i) {
ans = root;
return;
}
if (root == u) {
d = i;
return;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
}
};
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,15 @@ class Solution {
for (int i = q.size(); i; --i) {
root = q.front();
q.pop();
if (root == u) return i > 1 ? q.front() : nullptr;
if (root->left) q.push(root->left);
if (root->right) q.push(root->right);
if (root == u) {
return i > 1 ? q.front() : nullptr;
}
if (root->left) {
q.push(root->left);
}
if (root->right) {
q.push(root->right);
}
}
}
return nullptr;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@
*/
class Solution {
public:
TreeNode* u;
TreeNode* ans;
int d = 0;

TreeNode* findNearestRightNode(TreeNode* root, TreeNode* u) {
this->u = u;
TreeNode* ans;
int d = 0;
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int i) {
if (!root || ans) {
return;
}
if (d == i) {
ans = root;
return;
}
if (root == u) {
d = i;
return;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
};
dfs(root, 1);
return ans;
}

void dfs(TreeNode* root, int i) {
if (!root || ans) return;
if (d == i) {
ans = root;
return;
}
if (root == u) {
d = i;
return;
}
dfs(root->left, i + 1);
dfs(root->right, i + 1);
}
};
9 changes: 6 additions & 3 deletions solution/1600-1699/1603.Design Parking System/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,17 +100,20 @@ class ParkingSystem {
```cpp
class ParkingSystem {
public:
vector<int> cnt;

ParkingSystem(int big, int medium, int small) {
cnt = {0, big, medium, small};
}

bool addCar(int carType) {
if (cnt[carType] == 0) return false;
if (cnt[carType] == 0) {
return false;
}
--cnt[carType];
return true;
}

private:
vector<int> cnt;
};

/**
Expand Down
9 changes: 6 additions & 3 deletions solution/1600-1699/1603.Design Parking System/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,17 +92,20 @@ class ParkingSystem {
```cpp
class ParkingSystem {
public:
vector<int> cnt;

ParkingSystem(int big, int medium, int small) {
cnt = {0, big, medium, small};
}

bool addCar(int carType) {
if (cnt[carType] == 0) return false;
if (cnt[carType] == 0) {
return false;
}
--cnt[carType];
return true;
}

private:
vector<int> cnt;
};

/**
Expand Down
9 changes: 6 additions & 3 deletions solution/1600-1699/1603.Design Parking System/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
class ParkingSystem {
public:
vector<int> cnt;

ParkingSystem(int big, int medium, int small) {
cnt = {0, big, medium, small};
}

bool addCar(int carType) {
if (cnt[carType] == 0) return false;
if (cnt[carType] == 0) {
return false;
}
--cnt[carType];
return true;
}

private:
vector<int> cnt;
};

/**
Expand Down
Loading