Skip to content

Instantly share code, notes, and snippets.

@anil477
Created July 8, 2017 17:34
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 anil477/8ae975a2cf78dc2a0f6dd7e454b58deb to your computer and use it in GitHub Desktop.
Save anil477/8ae975a2cf78dc2a0f6dd7e454b58deb to your computer and use it in GitHub Desktop.
Find Count of Single Valued Subtrees
// https://www.quora.com/Write-a-program-to-count-the-number-of-uni-value-subtrees-in-a-given-tree/answer/Saichand-Neelapala-2?srid=uObe
static int count=0;
public static boolean findUni(BSTNode <Integer> node){
if(node == null) return true;
boolean left = findUni(node.left);
boolean right = findUni(node.right);
if(left && right && (node.left==null || node.left.data == node.data) &&
(node.right==null ||node.right.data == node.data)){
count++;
return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment