104. Maximum Depth of Binary Tree

题目:

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.

 

Hide Tags
  Tree Depth-first Search
 

链接: http://leetcode.com/problems/maximum-depth-of-binary-tree/

题解:

也是DFS。 假如不使用recursive,可以用level order traversal,使用queue来辅助,每增加一层,depth + 1。

Time Complexity - O(n), Space Complexity - O(n)。

public class Solution {

    public int maxDepth(TreeNode root) {

        if(root == null)

            return 0;

        return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));

    }

}

测试:

你可能感兴趣的:(binary)