Created
July 8, 2017 17:34
-
-
Save anil477/8ae975a2cf78dc2a0f6dd7e454b58deb to your computer and use it in GitHub Desktop.
Find Count of Single Valued Subtrees
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
// 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