Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. class Solution {
  2. static int height=-1;
  3. public int solution(Tree T) {
  4. if (T == null) return height;
  5. height = heightOfTree(T);
  6. return height;
  7. }
  8.  
  9. public int heightOfTree(Tree node) {
  10. if (node != null) {
  11. if (node.l == null) {
  12. return heightOfTree(node.r);
  13. }
  14. if (node.r == null) {
  15. return 1 + heightOfTree(node.l);
  16. } else {
  17. return 1 + Math.max(heightOfTree(node.r), heightOfTree(node.l));
  18. }
  19. }
  20. return 0;
  21. }
  22.  
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement