https://oj.leetcode.com/problems/maximum-depth-of-binary-tree/
http://blog.csdn.net/linhuanmars/article/details/19659525
/** * definition for binary tree * public class treenode { * int val; * TreeNode left; * treenode right; * treenode (int &NBSP;X) { val = x; } * } */public class solution { public int maxdepth (treenode root) { // Solution A: return maxdepth_dfs (root, 0); // solution b: // return maxdepth_bfs (Root); } // solution a: dfs // Private int maxdepth_dfs (treenode node, int lastdepth) { if (node == null) return lastDepth; Return math.max (Maxdepth_dfs (node.left, lastdepth + 1), maxdepth_dfs (Node.right, lastdepth + 1)); } ///// // solution b: bfs // &NBSP;&NBSP;&NBSP;&NBSP;PRIVATE&NBSP;INT&NBSP;MAXDEPTH_BFS (treenode root) { if (root == null) return 0; // Use node.val as its depth root.val = 1; queue<treenode> queue = new LinkedList<> (); queue.offer (root); int max = 0; while (!queue.isempty ()) { Treenode node = queue.poll (); if (node.left == Null && noDe.right == null && node.val > max) { max = node.val; } if (node.left != null) { node.left.val = node.val + 1; queue.offer (Node.left); } if (node.right != null) { node.right.val = node.val + 1; queue.offer (node.right); } } return max; }}
[leetcode]104 Maximum Depth of Binary Tree