add: 220524-CN [cpp]
This commit is contained in:
parent
c4334f67cc
commit
26d14ac79c
|
|
@ -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);
|
||||
}
|
||||
};
|
||||
|
|
@ -3,4 +3,4 @@ PROJECT(2205)
|
|||
|
||||
SET(CMAKE_CXX_STANDARD 23)
|
||||
|
||||
ADD_EXECUTABLE(2205 220523.cpp)
|
||||
ADD_EXECUTABLE(2205 220524-CN.cpp)
|
||||
|
|
|
|||
Loading…
Reference in New Issue