[Java代码] [Leetcode] Maximum and Minimum Depth of Binary Tree 二叉树的最小最大深度

来源:互联网 发布:我国域名总数2016年6月 编辑:程序博客网 时间:2024/05/17 00:17
Maximum Depth of Binary Tree

Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

递归法 复杂度

时间 O(N) 空间 O(H) 递归栈空间

思路

简单的递归。递归条件是,它的最大深度是它左子树的最大深度和右子树最大深度中较大的那个加1。基础条件是,当遇到空节点时,返回深度为0。该递归的实质是深度优先搜索。

代码

public class Solution {

  1. public int maxDepth(TreeNode root) {
  2. if(root == null){
  3. return 0;
  4. }http://www.nvzi91.cn/fujianyan/29948.html
  5. int left = maxDepth(root.left);
  6. int right = maxDepth(root.right);
  7. return Math.max(left, right) + 1;
  8. }http://www.nvzi91.cn/chunvmoxiufu/29949.html
复制代码

}

Minimum Depth of Binary Tree

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.

递归法 复杂度

时间 O(N) 空间 O(H) 递归栈空间

思路

当求最大深度时,我们只要在左右子树中取较大的就行了,然而最小深度时,如果左右子树中有一个为空会返回0,这时我们是不能算做有效深度的。所以分成了三种情况,左子树为空,右子树为空,左右子树都不为空。当然,如果左右子树都为空的话,就会返回1。

代码http://www.nvzi91.cn/niaodaoyan/29950.html
  1. public class Solution {
  2. public int minDepth(TreeNode root) {
  3. if(root == null){
  4. return 0;
  5. }http://www.nvzi91.cn/chunvmoxiufu/29951.html
  6. int depth = 0;
  7. if(root.left != null && root.right != null){
  8. int left = minDepth(root.left);
  9. int right = minDepth(root.right);
  10. depth = Math.min(left, right);
  11. } else if (root.left != null){
  12. depth = minDepth(root.left);
  13. } else if (root.right != null){
  14. depth = minDepth(root.right);
  15. }http://www.nvzi91.cn/yindaoyan/29952.html
  16. return depth + 1;
  17. }
  18. }
复制代码
广度优先搜索 复杂度

时间 O(N) 空间 O(B)

思路

递归解法本质是深度优先搜索,但因为我们是求最小深度,并不一定要遍历完全部节点。如果我们用广度优先搜索,是可以在遇到第一个叶子节点时就终止并返回当前深度的。

代码
  1. public class Solution {
  2. public int minDepth(TreeNode root) {
  3. Queue<TreeNode> queue = new LinkedList<TreeNode>();
  4. if(root!=null) queue.offer(root);
  5. int depth = 0;
  6. while(!queue.isEmpty()){
  7. int size = queue.size();
  8. depth++;http://www.kmrlyy.com/fujianyan/33465.html
  9. for(int i = 0; i < size; i++){
  10. TreeNode curr = queue.poll();
  11. if(curr.left == null && curr.right == null){
  12. return depth;
  13. }http://m.nvzi91.cn/gongjingjibing/29360.html
  14. if(curr.left != null) queue.offer(curr.left);
  15. if(curr.right != null) queue.offer(curr.right);
  16. }
  17. }
  18. return depth;
  19. }
  20. }http://www.kmrlyy.com/fujianyan/33466.html
复制代码
迭代加深有限深度优先搜索 复杂度

时间 O(N) 空间 O(D)

思路

英文名是Iterative Deepening Depth Limited Search.然而,广度优先搜索有一个致命缺陷就是,一旦分支变多,消耗空间就太大。这里我们还有改进的余地,就是用迭代加深的有限深度优先搜索。该算法每次迭代是一次有限深度优先搜索,如果本轮迭代没有发现目标(这题的目标是第一个叶子结点),则增加深度上限重新进行有限深度优先搜索。读者可能觉得这样会带来很多重复计算,但实际上经过数学证明后可以发现,该算法的时间复杂度和广度优先搜索是在一个数量级的,并没有太大的增加,而他的空间消耗仅仅是限制的深度。详情请翻阅Artificial Intelligence : A Modern Approach。

代码
  1. public class Solution {
  2. public int minDepth(TreeNode root) {
  3. if(root==null) return 0;http://www.kmrlyy.com/penqiangyan/33467.html
  4. return iterativeDeepeningDFS(root);
  5. }
  6. private boolean depthLimitedSearch(TreeNode root,int depth){
  7. boolean left = false;
  8. boolean right = false;
  9. if(depth>0){
  10. if(root.left==null&&root.right==null){
  11. return true;
  12. }http://www.kmrlyy.com/gongjingai/33468.html
  13. if(root.left!=null) {
  14. left = depthLimitedSearch(root.left, depth-1);
  15. }
  16. if(root.right!=null) {
  17. right = depthLimitedSearch(root.right, depth-1);
  18. }http://www.kmrlyy.com/gongjingai/33469.html
  19. return left||right;
  20. } else {
  21. return false;
  22. }
  23. }http://m.nvzi91.cn/gongjingfeida/29358.html
  24. private int iterativeDeepeningDFS(TreeNode root){
  25. int thisDepth = 1;
  26. boolean foundMin = false;
  27. while(true){
  28. foundMin = depthLimitedSearch(root,thisDepth);
  29. if(foundMin){
  30. return thisDepth;
  31. } else {http://m.nvzi91.cn/gongjingmilan/29359.html
  32. thisDepth++;
  33. }www.nvzi91.cn
  34. }www.kmrlyy.com
  35. }m.nvzi91.cn
  36. }
0 0
原创粉丝点击