|
2 | 2 |
|
3 | 3 | importcom.fishercoder.common.classes.TreeNode;
|
4 | 4 |
|
5 |
| -/** |
6 |
| - * 965. Univalued Binary Tree |
7 |
| - * |
8 |
| - * A binary tree is univalued if every node in the tree has the same value. |
9 |
| - * |
10 |
| - * Return true if and only if the given tree is univalued. |
11 |
| - * |
12 |
| - * Example 1: |
13 |
| - * |
14 |
| - * 1 |
15 |
| - * / \ |
16 |
| - * 1 1 |
17 |
| - * / \ \ |
18 |
| - * 1 1 1 |
19 |
| - * |
20 |
| - * Input: [1,1,1,1,1,null,1] |
21 |
| - * Output: true |
22 |
| - * |
23 |
| - * |
24 |
| - * Example 2: |
25 |
| - * 2 |
26 |
| - * / \ |
27 |
| - * 2 2 |
28 |
| - * / \ |
29 |
| - * 5 2 |
30 |
| - * |
31 |
| - * Input: [2,2,2,5,2] |
32 |
| - * Output: false |
33 |
| - * |
34 |
| - * |
35 |
| - * Note: |
36 |
| - * |
37 |
| - * The number of nodes in the given tree will be in the range [1, 100]. |
38 |
| - * Each node's value will be an integer in the range [0, 99]. |
39 |
| - */ |
40 | 5 | publicclass_965 {
|
41 |
| -publicstaticclassSolution1 { |
42 |
| -publicbooleanisUnivalTree(TreeNoderoot) { |
43 |
| -if (root ==null) { |
44 |
| -returntrue; |
45 |
| - } |
46 |
| -returndfs(root,root.val); |
47 |
| - } |
| 6 | +publicstaticclassSolution1 { |
| 7 | +publicbooleanisUnivalTree(TreeNoderoot) { |
| 8 | +if (root ==null) { |
| 9 | +returntrue; |
| 10 | +} |
| 11 | +returndfs(root,root.val); |
| 12 | +} |
48 | 13 |
|
49 |
| -privatebooleandfs(TreeNoderoot,intvalue) { |
50 |
| -if (root ==null) { |
51 |
| -returntrue; |
52 |
| - } |
53 |
| -if (root.val !=value) { |
54 |
| -returnfalse; |
55 |
| - } |
56 |
| -returndfs(root.left,value) &&dfs(root.right,value); |
| 14 | +privatebooleandfs(TreeNoderoot,intvalue) { |
| 15 | +if (root ==null) { |
| 16 | +returntrue; |
| 17 | + } |
| 18 | +if (root.val !=value) { |
| 19 | +returnfalse; |
| 20 | + } |
| 21 | +returndfs(root.left,value) &&dfs(root.right,value); |
| 22 | + } |
57 | 23 | }
|
58 |
| - } |
59 | 24 | }
|