Created
September 2, 2013 02:56
-
-
Save leepro/6408855 to your computer and use it in GitHub Desktop.
Counting Invisible Nodes of Binary Tree.
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
class Solution { | |
int total_nodes = 0; | |
int nonvisible_nodes = 0; | |
public int solution(Tree T) { | |
traverse(T, Integer.MIN_VALUE); | |
return total_nodes - nonvisible_nodes; | |
} | |
public void traverse(Tree T, int cur_max) | |
{ | |
if(T != null) | |
total_nodes++; | |
else | |
return; | |
if(T.x > cur_max) | |
cur_max = T.x; | |
if(T.x < cur_max) | |
nonvisible_nodes++; | |
traverse(T.l, cur_max); | |
traverse(T.r, cur_max); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment