[LeetCode]: 104: Maximum Depth of Binary Tree

本文详细介绍了使用深度优先搜索(DFS)和广度优先搜索(BFS)算法来计算二叉树的最大深度,并通过示例代码演示了实现过程。同时,文章还解释了泛型队列的基本概念及其在算法实现中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

DFS:

    public static int maxDepth_DFS(Node root) {
        
    	if(root == null){  
            return 0;  
        }
    	else{
            int intLeftTreeDepth = maxDepth_DFS(root.left);  
            int intRightTreeDepth = maxDepth_DFS(root.right);  
            
            return intLeftTreeDepth > intRightTreeDepth? intLeftTreeDepth + 1:intRightTreeDepth+ 1;  
    	}
    }

BFS:

    public static int maxDepth_BFS(Node root) {
        
        if(root == null){  
            return 0;  
        }
        
        Queue_Generic<Node> que = new Queue_Generic<Node>(1000);
        int nCount = 1;
        int nDepth = 0;// 记录队列里面每一层上的元素
        que.Insert(root);
        
        while(!que.isEmpty()) {

            Node NodeTemp = que.PeekFront();

            que.Remove();
            nCount --;

            //if(!NodeTemp.left.equals(null)){
            if(! (NodeTemp.left == null)){
                que.Insert(NodeTemp.left);
            }

            //if (!NodeTemp.right.equals(null)){
            if (!(NodeTemp.right==null)){
                que.Insert(NodeTemp.right);
            }

            if (nCount == 0) {
                //System.out.println("Node Count : "+nCount);
                nDepth ++;
                nCount = que.Size();
                //System.out.println("Node Count : "+nCount);
                
            }
            
        }
        return nDepth;
    }

 

其它知识

泛型队列:

public class Queue_Generic <T>{
    int MaxSize;
    T[] DataStorage;
    int iHeadPos;
    int iEndPos;
    int iItenCount;
    
    public Queue_Generic(int intMax){
        MaxSize = intMax;
        iHeadPos = 0;
        iEndPos = -1;
        iItenCount = 0;
        DataStorage = (T[]) new Object[intMax];
    }
    
    public void Insert(T value){
        if(MaxSize > iItenCount){
            if(iEndPos == MaxSize-1){
                iEndPos = -1;
            }
            DataStorage[++iEndPos] = value;
            iItenCount++;
        }
    }
    
    public T Remove(){
        T Temp;
        if(iItenCount > 0){
            iItenCount--;
            Temp = DataStorage[iHeadPos++];
          if(iHeadPos == MaxSize){
              iHeadPos = 0;
          }
        }
        else{
            return null;
        }
        return Temp;
    }
    
    public T Peek(){
        return (T)DataStorage[iEndPos];
    }
    
    public T PeekFront(){
        return (T)DataStorage[iHeadPos];
    }
    
    public boolean isEmpty(){
        if(iItenCount == 0){
            return true;
        }else{
            return false;
        }
    }
    
    public boolean isFull(){
        if(iItenCount == MaxSize){
            return true;
        }else{
            return false;
        }
    }
    
    public int Size(){
        return iItenCount;
    }
}

其中,关键的点

1. 存储结构需要将Object强制转换成泛型类型:DataStorage = (T[]) new Object[intMax];

2. 返回的结果为泛型类型: return (T)DataStorage[iEndPos];

转载于:https://www.cnblogs.com/savageclc26/p/4773180.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值