MaximumDepthBinaryTreeSolution { static int maxDepthFirstBreadth(TreeNode root) { if (root == null) return 0; Queue<TreeNode> q = new LinkedBlockingDeque<>(); q.add(root); int res = 0; TreeNode p; while (q.size() != 0) { ++res; for (int i = 0; i < q.size(); ++i) { p = q.poll(); if (p.left != null) q.add(p.left); if (p.right != null) q.add(p.right); } } return res; }    }
@Test public void maxDepthFirstBreadth() { assertEquals(0, MaximumDepthBinaryTreeSolution.maxDepthFirstBreadth(null)); TreeNode root0 = new TreeNode(0); assertEquals(1, MaximumDepthBinaryTreeSolution.maxDepthFirstBreadth(root0)); TreeNode left1 = new TreeNode(1); left1.left = new TreeNode(2); root0.left = left1; assertEquals(3, MaximumDepthBinaryTreeSolution.maxDepthFirstBreadth(root0)); }