题目链接
层序遍历
当遍历到二叉树每一层最后一个节点时,depth++
/*** Definition for a binary tree node.* public class TreeNode {* int val;* TreeNode left;* TreeNode right;* TreeNode() {}* TreeNode(int val) { this.val = val; }* TreeNode(int val, TreeNode left, TreeNode right) {* this.val = val;* this.left = left;* this.right = right;* }* }*/
class Solution {public int maxDepth(TreeNode root) {int depth = 0;Deque<TreeNode> queue = new LinkedList<TreeNode>();if(root != null){queue.offer(root);}while(!queue.isEmpty()){int size = queue.size();for(int i = 0; i < size; i++){TreeNode node = queue.poll();if(node.left != null){queue.offer(node.left);}if(node.right != null){queue.offer(node.right);}if(i == size - 1){depth++;}}}return depth;}
}