We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 33e4af5 commit 0741b3bCopy full SHA for 0741b3b
solution/0965.Univalued Binary Tree/Solution.cpp
@@ -0,0 +1,30 @@
1
+/**
2
+ * Definition for a binary tree node.
3
+ * struct TreeNode {
4
+ * int val;
5
+ * TreeNode *left;
6
+ * TreeNode *right;
7
+ * TreeNode(int x) : val(x), left(NULL), right(NULL) {}
8
+ * };
9
+ */
10
+class Solution {
11
+private:
12
+ bool isUnivalVal(TreeNode* r, int val)
13
+ {
14
+ if (val != r->val)
15
+ return false ;
16
+
17
+ if (r->left && !isUnivalVal(r->left, val))
18
19
+ if (r->right && !isUnivalVal(r->right, val))
20
21
22
+ return true ;
23
+ }
24
+public:
25
+ bool isUnivalTree(TreeNode* root) {
26
+ if (nullptr == root)
27
28
+ return isUnivalVal(root, root->val) ;
29
30
+} ;
0 commit comments