diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md index 3b0c1f691fec0..7e1dc66141450 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README.md @@ -61,7 +61,7 @@ ### 方法一:BFS -BFS 层序遍历,找到 $u$ 所在层的右侧相邻节点。 +我们可以使用广度优先搜索,从根节点开始搜索,当搜索到节点 $u$ 时,返回队列中的下一个节点。 时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是二叉树的节点个数。 @@ -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; @@ -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 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); - } }; ``` diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md index 22680cf8ebbb7..5428e0d623817 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/README_EN.md @@ -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. @@ -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; @@ -200,7 +210,11 @@ var findNearestRightNode = function (root, u) { -### 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. @@ -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 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); - } }; ``` diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp index 8240d0965a116..dadc3f2e66e36 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution.cpp @@ -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; diff --git a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution2.cpp b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution2.cpp index 392d899d1a1a6..40c810d77f4f3 100644 --- a/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution2.cpp +++ b/solution/1600-1699/1602.Find Nearest Right Node in Binary Tree/Solution2.cpp @@ -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 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); - } }; \ No newline at end of file diff --git a/solution/1600-1699/1603.Design Parking System/README.md b/solution/1600-1699/1603.Design Parking System/README.md index 2e74edc9cec48..ac9127c4ad60d 100644 --- a/solution/1600-1699/1603.Design Parking System/README.md +++ b/solution/1600-1699/1603.Design Parking System/README.md @@ -100,17 +100,20 @@ class ParkingSystem { ```cpp class ParkingSystem { public: - vector 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 cnt; }; /** diff --git a/solution/1600-1699/1603.Design Parking System/README_EN.md b/solution/1600-1699/1603.Design Parking System/README_EN.md index fb843f1033b3d..7662a1a1054a8 100644 --- a/solution/1600-1699/1603.Design Parking System/README_EN.md +++ b/solution/1600-1699/1603.Design Parking System/README_EN.md @@ -92,17 +92,20 @@ class ParkingSystem { ```cpp class ParkingSystem { public: - vector 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 cnt; }; /** diff --git a/solution/1600-1699/1603.Design Parking System/Solution.cpp b/solution/1600-1699/1603.Design Parking System/Solution.cpp index bba7bbbda3b41..d5c08374e07a5 100644 --- a/solution/1600-1699/1603.Design Parking System/Solution.cpp +++ b/solution/1600-1699/1603.Design Parking System/Solution.cpp @@ -1,16 +1,19 @@ class ParkingSystem { public: - vector 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 cnt; }; /**