본문 바로가기

알고리즘/해커랭크

[해커랭크] Tree: Height of a Binary Tree [JAVA]

문제링크 : www.hackerrank.com/challenges/tree-height-of-a-binary-tree/problem

 

Tree: Height of a Binary Tree | HackerRank

Given a binary tree, print its height.

www.hackerrank.com

재귀를 한단계 수행할 때마다 리턴받을 값이 하나씩 증가하게 된다

public static int height(Node root) {
        if(root==null)return -1;
      	int ret = 0;
        ret = Math.max(height(root.left),height(root.right));
        return ret+1;
    }