Submission Detail

82 / 82 test cases passed.
Status:

Accepted

Runtime: 9 ms
Memory Usage: 44.5 MB
Submitted: 0 minutes ago
loading
Runtime Error Message:
Last executed input:
Input:
Output:
Expected:

Accepted Solutions Runtime Distribution

0
1
2
3
4
5
6
7
8
9
10
11
12
20
40
60
80
java
You are here!
Your runtime beats 5.75 % of java submissions.
Runtime (ms)
Distribution (%)

0
1
2
3
4
5
6
7
8
9
10
11
12
25
50
75
Zoom area by dragging across this chart

Accepted Solutions Memory Distribution

41500
42000
42500
43000
43500
44000
2
4
6
8
10
java
Memory (KB)
Distribution (%)

41500
42000
42500
43000
43500
44000
5
10
Zoom area by dragging across this chart

Invite friends to challenge Validate Binary Search Tree


Submitted Code: 0 minutes ago

Language: 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
61
62
63
64
65
66
67
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isValidBST(TreeNode root) {
boolean BST = true;
return checkTree(root,BST);
}
public boolean checkTree(TreeNode r, boolean BST) {
if (r == null || BST == false)
return BST;
if (r.left != null)
if (r.left.val >= r.val)
BST = false;
if (r.right != null)
if (r.right.val <= r.val)
BST = false;
ArrayList<Integer> inorder = new ArrayList<>();
inorder = inOrderTraversal(r,inorder);
int j = 0;
while (inorder.get(j) != r.val) {
if (inorder.get(j) >= r.val)
return false;
j++;
}
j++;
while (j < inorder.size()) {
if (inorder.get(j) <= r.val)
return false;
j++;
}
BST = checkTree(r.left, BST);
BST = checkTree(r.right, BST);
return BST;
}
public ArrayList<Integer> inOrderTraversal(TreeNode root, ArrayList<Integer> iot) {
if (root == null)
return iot;
iot = inOrderTraversal(root.left,iot);
iot.add(root.val);
iot = inOrderTraversal(root.right,iot);
return iot;
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX