Skip to content

Instantly share code, notes, and snippets.

@felipediogo
Last active June 20, 2018 00:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felipediogo/d58e8f529515378deef02b9b7bbe4e2c to your computer and use it in GitHub Desktop.
Save felipediogo/d58e8f529515378deef02b9b7bbe4e2c to your computer and use it in GitHub Desktop.
/* 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