import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
/**
* 给定一个二叉树,找出其最大深度。
* 二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。
* 说明: 叶子节点是指没有子节点的节点。
* 示例:
* 给定二叉树 [3,9,20,null,null,15,7],
*
* 3
* / \
* 9 20
* / \
* 15 7
*
* 返回它的最大深度 3 。
*
* 来源:力扣(LeetCode)
* 链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
* 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。
*/
class Solution {
/**
* DFS:深度优先遍历解法
*
* @param root
* @return
*/
public int maxDepth2(TreeNode root) {
return root == null ? 0 : Math.max(maxDepth2(root.left), maxDepth2(root.right)) + 1;
}
/**
* BFS:广度优先遍历解法
*
* @param root
* @return
*/
public int maxDepth1(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> treeNodeQueue = new ArrayDeque<>();
treeNodeQueue.add(root);
int level = 1;
while (!treeNodeQueue.isEmpty()) {
List<TreeNode> currentLevelNodes = new ArrayList<>();
while (!treeNodeQueue.isEmpty()) {
TreeNode node = treeNodeQueue.poll();
if (node.left != null) {
currentLevelNodes.add(node.left);
}
if (node.right != null) {
currentLevelNodes.add(node.right);
}
}
if (currentLevelNodes.size() != 0) {
level++;
for (TreeNode treeNode : currentLevelNodes) {
treeNodeQueue.add(treeNode);
}
}
}
return level;
}
/**
* BFS:广度优先遍历解法2
* @param root
* @return
*/
public int maxDepth(TreeNode root) {
if (root == null) {
return 0;
}
Queue<TreeNode> treeNodeQueue = new ArrayDeque<>();
treeNodeQueue.add(root);
int level =0;
while (!treeNodeQueue.isEmpty()) {
level++;
int currentLevelSize = treeNodeQueue.size();
for (int i =0;i< currentLevelSize;i++){
TreeNode node = treeNodeQueue.poll();
if (node.left != null) {
treeNodeQueue.add(node.left);
}
if (node.right != null) {
treeNodeQueue.add(node.right);
}
}
}
return level;
}
}
public class MainClass {
public static TreeNode stringToTreeNode(String input) {
input = input.trim();
input = input.substring(1, input.length() - 1);
if (input.length() == 0) {
return null;
}
String[] parts = input.split(",");
String item = parts[0];
TreeNode root = new TreeNode(Integer.parseInt(item));
Queue<TreeNode> nodeQueue = new LinkedList<>();
nodeQueue.add(root);
int index = 1;
while (!nodeQueue.isEmpty()) {
TreeNode node = nodeQueue.remove();
if (index == parts.length) {
break;
}
item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int leftNumber = Integer.parseInt(item);
node.left = new TreeNode(leftNumber);
nodeQueue.add(node.left);
}
if (index == parts.length) {
break;
}
item = parts[index++];
item = item.trim();
if (!item.equals("null")) {
int rightNumber = Integer.parseInt(item);
node.right = new TreeNode(rightNumber);
nodeQueue.add(node.right);
}
}
return root;
}
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line;
while ((line = in.readLine()) != null) {
TreeNode root = stringToTreeNode(line);
int ret = new Solution().maxDepth(root);
String out = String.valueOf(ret);
System.out.print(out);
}
}
}
核心思想:
1:DFS:深度优先遍历-递归解法
2:BFS:过度优先遍历-迭代解法
- 记录每层树的结果,每次将当前层最后再加入到队列中。当前队列遍历完毕,层数+1
- 记录每层树的个数,循环到指定个数进入下一层