We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
思路:遍历每一层的时候,记录次数;当该节点的左右节点都为空的时候,直接返回当前的深度即可,如果左右节点不为空就加入队列
class Solution { public int minDepth(TreeNode root) { if (root == null) { return 0; } Queue<TreeNode> que = new LinkedList<>(); que.offer(root); int min = 0; while (!que.isEmpty()) { int len = que.size(); min++; for (int i = 0; i < len; i++) { TreeNode node = que.poll(); if (node.left == null && node.right == null) { return min;//该节点的左右节点大都为空直接返回当前的深度 } //加入该节点的左右节点 if (node.left != null) que.offer(node.left); if (node.right != null) que.offer(node.right); } } return min; } }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
111. 二叉树的最小深度
The text was updated successfully, but these errors were encountered: