Last active
June 20, 2018 00:49
-
-
Save felipediogo/d58e8f529515378deef02b9b7bbe4e2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Hidden stub code will pass a root argument to the function below. Complete the function to solve the challenge. Hint: you may want to write one or more helper functions. | |
The Node class is defined as follows: | |
class Node { | |
int data; | |
Node left; | |
Node right; | |
} | |
*/ | |
boolean checkBST(Node root) { | |
ArrayList numbers = checkNode(root.left, new ArrayList()); | |
numbers.add(root.data); | |
numbers.addAll(checkNode(root.right, new ArrayList())); | |
return isSorted(numbers); | |
} | |
public boolean isSorted(ArrayList<Integer> list) | |
{ | |
boolean sorted = true; | |
for (int i = 1; i < list.size(); i++) { | |
if (list.get(i-1) >= list.get(i)) sorted = false; | |
} | |
return sorted; | |
} | |
public ArrayList checkNode(Node node, ArrayList list) { | |
if (node.left != null) { | |
checkNode(node.left, list); | |
} | |
list.add(node.data); | |
if (node.right != null) { | |
checkNode(node.right, list); | |
} | |
return list; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment