1 Star 0 Fork 0

yuhang2__2/LeetCode-Solutions

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
check-if-a-string-is-a-valid-sequence-from-root-to-leaves-path-in-a-binary-tree.cpp 2.32 KB
一键复制 编辑 原始数据 按行查看 历史
// Time: O(n)
// Space: O(w)
/**
* 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) {}
* };
*/
// bfs solution
class Solution {
public:
bool isValidSequence(TreeNode* root, vector<int>& arr) {
vector<TreeNode *> q = {root};
for (int depth = 0; depth < arr.size(); ++depth) {
vector<TreeNode *> new_q;
while (!q.empty()) {
const auto node = q.back(); q.pop_back();
if (!node || node->val != arr[depth]) {
continue;
}
if (depth + 1 == arr.size() && node->left == node->right) {
return true;
}
new_q.emplace_back(node->left);
new_q.emplace_back(node->right);
}
q = move(new_q);
}
return false;
}
};
// Time: O(n)
// Space: O(h)
// dfs solution with stack
class Solution2 {
public:
bool isValidSequence(TreeNode* root, vector<int>& arr) {
vector<pair<TreeNode *, int>> s = {{root, 0}};
while (!s.empty()) {
const auto [node, depth] = s.back(); s.pop_back();
if (!node || depth == arr.size() || node->val != arr[depth]) {
continue;
}
if (depth + 1 == arr.size() && node->left == node->right) {
return true;
}
s.emplace_back(node->right, depth + 1);
s.emplace_back(node->left, depth + 1);
}
return false;
}
};
// Time: O(n)
// Space: O(h)
// dfs solution with recursion
class Solution3 {
public:
bool isValidSequence(TreeNode* root, vector<int>& arr) {
return dfs(root, arr, 0);
}
private:
bool dfs(TreeNode *node, const vector<int>& arr, int depth) {
if (!node || depth == arr.size() || node->val != arr[depth]) {
return false;
}
if (depth + 1 == arr.size() && node->left == node->right) {
return true;
}
return dfs(node->left, arr, depth + 1) || dfs(node->right, arr, depth + 1);
}
};
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/yuhang2__2/LeetCode-Solutions.git
git@gitee.com:yuhang2__2/LeetCode-Solutions.git
yuhang2__2
LeetCode-Solutions
LeetCode-Solutions
master

搜索帮助