Thursday, June 9, 2016

TreeHeight in Java & C# - Codility

Task : https://codility.com/programmers/task/tree_height/

Solution for Java :
1
2
3
4
5
6
7
8
class Solution {
    public int solution(Tree T) {
        // write your code in Java SE 8
        if(T == null) return -1;
  
  return 1+Math.max(solution(T.l), solution(T.r));
    }
}

Result :
Solution for C# :
1
2
3
4
5
6
public int solution(Tree T) {
        // write your code in C# 6.0 with .NET 4.5 (Mono)
        if(T == null) return -1;
  
  return 1+Math.Max(solution(T.l), solution(T.r));
    }

Result :

No comments:

Post a Comment