c_965_Univalued_Binary_Tree

//
// Created by Mr.Hu on 2019/1/3.
//
// leetcode 965 univalued binary tree
//
// 使用类似于DFS/BFS遍历的思想
//

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
using namespace std;

struct TreeNode {
int val;
TreeNode *left;
TreeNode *right;
TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

class Solution {
public:
bool isUnival = true;

bool isUnivalTree(TreeNode *root) {
dfs(root, root->val);
return isUnival;
}

bool dfs(TreeNode *root, int target) {
if (isUnival) {
if (root->val != target) {
isUnival = false;
} else {
if (root->left) dfs(root->left, target);
if (root->right) dfs(root->right, target);
}
}
return isUnival;
}


bool isUnivalTree_optimal(TreeNode *root) {
return dfs_optimal(root, root->val);
}

bool dfs_optimal(TreeNode *node, int target) {
if (!node) return true;
if (node->val != target) return false;
return dfs(node->left, target) && dfs(node->right, target);
}
};

int main() {
TreeNode a(1);
TreeNode b(1);
TreeNode c(1);
a.left = &b;
a.right = &c;
Solution solution;
// bool result = solution.isUnivalTree(&a);
bool result = solution.isUnivalTree_optimal(&a);
cout << result << endl;
return 0;
}