add: 220524-CN [cpp]

This commit is contained in:
Eat-Swap 2022-05-24 20:20:42 +08:00
parent c4334f67cc
commit 26d14ac79c
Signed by: Eatswap
GPG Key ID: BE661106A1F3FA0B
2 changed files with 28 additions and 1 deletions

27
cpp/2205/220524-CN.cpp Normal file
View File

@ -0,0 +1,27 @@
#include <functional>
struct TreeNode {
int val;
TreeNode* left;
TreeNode* right;
explicit TreeNode(int x = 0, TreeNode* l = nullptr, TreeNode* r = nullptr) : val(x), left(l), right(r) {}
};
/**
* 965. Univalued Binary Tree
* A binary tree is uni-valued if every node in the tree has the same value.
* Given the root of a binary tree, return true if the given tree is uni-valued, or false otherwise.
*/
class Solution {
public:
static bool isUnivalTree(TreeNode* root) {
if (!root)
return true;
std::function<bool(const TreeNode*)> dfs = [&](const TreeNode* ptr) {
return !ptr || (ptr->val == root->val && dfs(ptr->left) && dfs(ptr->right));
};
return dfs(root->left) && dfs(root->right);
}
};

View File

@ -3,4 +3,4 @@ PROJECT(2205)
SET(CMAKE_CXX_STANDARD 23)
ADD_EXECUTABLE(2205 220523.cpp)
ADD_EXECUTABLE(2205 220524-CN.cpp)