1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| public class Solution { public int treeDepth2(BinaryTreeNode root) { if (root == null) return 0;
BinaryTreeNode current = null; LinkedList<BinaryTreeNode> queue = new LinkedList<BinaryTreeNode>(); queue.offer(root); int cur, last; int level = 0; while (!queue.isEmpty()) { cur = 0; last = queue.size(); while (cur < last) { current = queue.poll(); cur++; if (current.left != null) { queue.offer(current.left); } if (current.right != null) { queue.offer(current.right); } } level++; } return level; } }
|