Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.
给定一个二叉树,找到它的最小深度。最小深度是从根节点到最近叶节点的最短路径上的节点数。
二叉树数据结构:
递归解法:深度优先遍历(DFS)
1.如果为空树,返回0
2.如果左节点为空,返回右节点的最小深度+1(根节点深度)
3.如果右节点为空,返回左节点的最小深度+1
4.如果左右节点均不为空,返回左右节点最小深度较小的一个+1
非递归解法:广度优先遍历(BFS)
层序遍历,找到第一个叶子节点返回(左右节点均为空即为叶子节点)