leetcode [Maximum Depth of Binary Tree]

//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;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值