Skip to content

Instantly share code, notes, and snippets.

@leepro
Created September 2, 2013 02:56
Show Gist options
  • Save leepro/6408855 to your computer and use it in GitHub Desktop.
Save leepro/6408855 to your computer and use it in GitHub Desktop.
Counting Invisible Nodes of Binary Tree.
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