2014年4月22日 星期二

[LeetCode] Maximum Depth of Binary Tree

Problem:
Given a binary tree, find its maximum depth.
The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
Solution:O(n)
public class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null)
            return 0;
        else {
            int leftheight = maxDepth(root.left);
            int rightheight = maxDepth(root.right);
            int height = Math.max(leftheight,rightheight)+1;
            return height;
        }    
    }
}

沒有留言:

張貼留言