//DFS递归
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
//The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node
//说明maxDepth计算的是最长路径上的结点个数
//设计递归算法时只用考虑到最开始的一层,然后找到递归出口即可
public int maxDepth(TreeNode root) {
if(root == null) return 0;//递归出口
else return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}
}
一种未完成的DFS迭代思路:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
//解法二(非递归算法):采用深度优先遍历的思想,用maxDepth来存放最终的maxDepth,
//用thisMax来与maxDepth比较,maxDepth = max{maxDepth, thisMax},
//thisMax的计算方法,压入一个子结点,thisMax++,弹出一个结点thisMax--;
//同时需要一个数来保存上一层的数,因为弹出后压入右侧结点时返回时弹出的结点已经弹出,控制不了thisMax--了
//这种方法只能计算出maxDepth,但无法算出具体的最长路径上的结点,
//如果要求最长路径上的结点,先求出maxDepth,在深度优先遍历时比较thisMax与maxDepth,
//若相等,则结束,则求出从左到右第一条最长路径上的结点
//树的深度优先遍历,先在栈中压入根结点,根结点的左子结点,左子结点的左子结点...直到左子结点为空
//然后弹栈,判断右子结点是否为空,若不为空,重复上一行;若为空,继续弹栈
public int maxDepth(TreeNode root) {
if(root == null) return 0;
Stack<TreeNode> stack = new Stack<>();
int maxDepth = 0;
int thisMax = 0;
TreeNode temp = root;
while(temp != null){
stack.push(temp);
thisMax++;//压栈thisMax++
if(thisMax > maxDepth) maxDepth = thisMax;
temp = temp.left;
while(!stack.isEmpty()){
temp = stack.pop();
thisMax--;//弹栈thisMax--
if(temp.right != null){//***
thisMax++;//当弹出结点的右子结点不为空时,thisMax++,因为之前递减了,这里要加回来
temp = temp.right;
break;//当弹出结点的右子结点不为空时,重复最外层循环
}
//这里有一个问题,右子结点重复完了后跳回到上一层时thisMax不好控制,用一个count来记录上一层也不好使
//所以当"***"时重复只能通过递归完成?
}
}
return maxDepth;
}
}
DFS迭代:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) { val = x; }
}
class Solution {
//解法二(非递归算法):采用深度优先遍历的思想,用maxDepth来存放最终的maxDepth,
//用thisMax来与maxDepth比较,maxDepth = max{maxDepth, thisMax},
//thisMax的计算方法,压入一个子结点,thisMax++,弹出一个结点thisMax--;
//同时需要一个数来保存上一层的数,因为弹出后压入右侧结点时返回时弹出的结点已经弹出,控制不了thisMax--了
//这种方法只能计算出maxDepth,但无法算出具体的最长路径上的结点,
//如果要求最长路径上的结点,先求出maxDepth,在深度优先遍历时比较thisMax与maxDepth,
//若相等,则结束,则求出从左到右第一条最长路径上的结点
//树的深度优先遍历,先在栈中压入根结点,根结点的左子结点,左子结点的左子结点...直到左子结点为空
//然后弹栈,判断右子结点是否为空,若不为空,重复上一行;若为空,继续弹栈
public int maxDepth(TreeNode root) {
//DFS迭代(有点BFS的意思)
//解决上面的层数不好控制时,用一个栈来存放从根结点到每个结点的路径长度,实时跟踪对应存放该结点的栈
//stack压入Node,则value也压入从根结点到Node的路径长度,stack弹出时value也弹出
if(root == null) return 0;
Stack<TreeNode> stack = new Stack<>();
Stack<Integer> value = new Stack<>();
stack.push(root);
value.push(1);//stack与value同步压入
int maxDepth = 0;
while(!stack.isEmpty()){
TreeNode node = stack.pop();
int temp = value.pop();//stack与value同步弹出
maxDepth = Math.max(temp, maxDepth);
if(node.right != null){//先压入右结点更像是DFS
stack.push(node.right);
value.push(temp + 1);
}
if(node.left != null){
stack.push(node.left);
value.push(temp + 1);
}
}
return maxDepth;
}
}BFS迭代:/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
public class Solution {
public int maxDepth(TreeNode root) {
//BFS迭代(一层一层的往下找长度,层次遍历是用Queue存)
if(root == null) return 0;
Queue<TreeNode> queue = new LinkedList<>();
TreeNode temp = null;
int maxDepth = 0;
queue.add(root);
while(!queue.isEmpty()){
maxDepth++;//只要queue非空,说明该层有元素,则maxDepth++
for(int i = 0, n = queue.size(); i < n; i++){//用queue.size要先存起来,因为每次弹了后queue大小会减小
//将每一层的队列里的结点弹出来,再将弹出的每一个结点的下一层放入队列中
temp = queue.poll();
if(temp.left != null)
queue.add(temp.left);
if(temp.right != null)
queue.add(temp.right);
}
}
return maxDepth;
}
}
本文介绍了计算二叉树最大深度的两种算法:递归DFS和迭代BFS,并详细解析了每种方法的具体实现过程。
1409

被折叠的 条评论
为什么被折叠?



