-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJudgeIt.java
61 lines (50 loc) · 1.16 KB
/
JudgeIt.java
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
56
57
58
59
60
import java.util.*;
/*
* public class TreeNode {
* int val = 0;
* TreeNode left = null;
* TreeNode right = null;
* }
*/
public class JudgeIt {
/**
*
* @param root TreeNode类 the root
* @return bool布尔型一维数组
*/
long pre = Long.MIN_VALUE;
public boolean[] judgeIt (TreeNode root) {
// write code here
boolean[] res = new boolean[2];
res[0] = isSearchTree(root);
res[1] = isPerfect(root);
return res;
}
public boolean isSearchTree(TreeNode root){
if(root == null){
return true;
}
if(!isSearchTree(root.left)){
return false;
}
if(root.val <= pre){
return false;
}
pre = root.val;
return isSearchTree(root.right);
}
int n = 0;
int p = 0;
public boolean isPerfect(TreeNode root){
if(!dfs(root,1)) return false;
return n==p;
}
public boolean dfs(TreeNode root,int k){
if(root == null){
return true;
}
n++;
p=Math.max(p,k);
return dfs(root.left,2*k)&&dfs(root.right,2*k+1);
}
}