Leedcode题目总结-TREE-求深度问题

Pro1(求最大深度问题):

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.


解题思路:

因为求最大深度,即从跟节点到最远的叶节点。

考虑使用DSP(深度优先算法)利用递归实现;

考虑使用BSP(广度优先算法)利用迭代实现:这里实现主要思路是,只要每一个level至少存在一个子节点,树的深度就加1。


解决方法:

1递归实现

public class Solution {
    public int maxDepth(TreeNode root) {
        if(root == null) return 0;

        int i = maxDepth(root.left);
        int j = maxDepth(root.right);
        return Math.max(i,j) + 1; 
    }
}

2迭代实现

 public class Solution{
     public int maxDepth(TreeNode root) {
        if(root == null) return 0;

        ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
        queue.add(root);
        int count = 0;
        while(!queue.isEmpty()){
        int size = queue.size();
            while(size > 0){
                TreeNode x = queue.remove();
                if(x.left != null) {
                    queue.add(x.left);
          } 
                if(x.right != null){
                    queue.add(x.right);
              
          }
                size--;   
      }
            count++;
    }
        return count;

  }
}

3BSP方法另一种实现方式

public class Solution{
     public int maxDepth(TreeNode root){
         if(root == null) return 0;

         ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
         queue.add();
         int level = 0;
         int curNum = 1; //当前level上存在的节点个数
         int NextNum = 0;//下一个level上存在的节点个数

         while(!queue.isEmpty()){
             TreeNode x = queue.remove();
             curNum--;
       
             if(x.left != 0) {
                 queue.add(x.left);
                 NextNum++;
        }
             if(x.right != 0){
                 queue.add(x.right);
                 NextNum++;
        }
             if(curNum == 0){
                 curNum = NextNum;
                 NextNum = 0;
                 level++;
        }
      
    }

  }
}

Pro2:

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.(跟最大深度一样,解题思路类似)


解题思路:

最小深度就是根节点到最近叶子节点的路径长度
和找二叉树的最大深度类似,但是区别如下:
递归终止条件:
1.根节点为null时,返回0
2.根节点左右都null时,返回1
3.大返回终止条件:返回较小值+1


Solution1(递归):

public class Solution{
    public int minDepth(TreeNode root){
        if(root == null) return 0;

        if(root.right == null) return minDepth(root.left) + 1;
        if(root.left == null) return minDepth(root.right) + 1;

        return Math.min(minDepth(root.left) , minDepth(root.right)) + 1;

  }
}
或者这样写:

public class Solution {
    public int minDepth(TreeNode root) {
        if(root == null) return 0;
        
        int i = minDepth(root.left);
        int j = minDepth(root.right);
        
        if(i == 0 || j == 0) return Math.max(i,j) + 1;
        return Math.min(i,j) + 1;
        
    }
}

Solution2(BSP迭代):

public class Solution{
    public int minDepth(TreeNode root){
        if(root == null) return 0;

        ArrayDeque<TreeNode> queue = new ArrayDeque<TreeNode>();
        int level = 1;  
        queue.add(root);
        while(!queue.isEmpty()){
            int size = queue.size(); 
            while(size != 0){
                TreeNode x = queue.remove();
                if(x.left != null) queue.add(x.left);
                if(x.right != null) queue.add(x.right);
                if(x.left == null && x.right == null) return level; //这里对于最小深度只要当一个节点没有后续子节点,那个就是最小深度
                size--; //只要当size为0就进入下一个level
      }
            level++;
    } 
        return level;     
  }

}

总结一下:

对于求深度的问题,如果利用非递归的方法,首先要考虑用深度优先还是广度优先;其次,深度可以跟level挂钩,此时要考虑如何计算level,在这里queue的size代表树中有多少个节点,可以利用size判断是否进入下一个level。当然也可以用两个指针来第一个指针cur记录当前level的节点个数,第二个指针next来记录下一个level的节点个数。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值