求二叉树的最大深度的两种思路及JAVA代码实现

import java.util.LinkedList;

public class MaxDepth {
    public static void main(String[] args) {


    }
     //思路1
     //递归求解树的最大深度
     //树的深度等于左子树的深度和右子树的深度中的最大值 + 1
    public int maxDepth(TreeNode root) {
        if (root == null) {
            return 0;//空树的深度为0
        }
        int leftHeight = maxDepth(root.left);
        int rightHeight = maxDepth(root.right);
        return Math.max(leftHeight, rightHeight) + 1;//树的深度等于左子树的深度和右子树的深度中的最大值 + 1
    }

    //思路2:广度优先遍历
    //广度优先搜索的队列里存放的是「当前层的所有节点」。
    // 每次拓展下一层的时候,不同于广度优先搜索的每次只从队列里拿出一个节点,我们需要将队列里的所有节点都拿出来进行拓展,
    // 这样能保证每次拓展完的时候队列里存放的是当前层的所有节点,即我们是一层一层地进行拓展,
    // 最后我们用一个变量 ans来维护拓展的次数,该二叉树的最大深度即为 ans

    public int maxDepthByBFS(TreeNode root) {
        if (root == null) {
            return 0;
        }

        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.offer(root);//加入根节点
        int depth = 0;
        while (!queue.isEmpty()) {
            int size = queue.size(); //需要将队列里的所有节点都拿出来进行拓展
            while (size > 0) {

                TreeNode node = queue.poll();
                if (node.left != null) {
                    queue.offer(node.left);//加入下一层的结点
                }
                if (node.right != null) {
                    queue.offer(node.right);加入下一层的结点
                }

                size --;
            }
            //没遍历完一层,树的深度 + 1
            depth ++;
        }

        return depth;
    }
}

//树的结点类
public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    public TreeNode(int val) {
        this.val = val;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是Java二叉树深度两种方法: 方法一:递归法 在递归方法中,我们首先检查根节点是否为空。如果为空,说明二叉树为空树,深度为0。然后,我们使用递归的方式分别计算左子树和右子树的最大深度。最后,我们返回左子树和右子树中深度较大的值加1,即为二叉树最大深度。 ```java public class Solution { public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } //递归版本 public int TreeDepth(TreeNode root) { if(root==null) return 0; int nLeft=TreeDepth(root.left); int nRight=TreeDepth(root.right); return (nLeft>nRight)?(nLeft+1):(nRight+1); } } ``` 方法二:非递归法 在非递归方法中,我们使用一个栈来存储每个节点以及它们的深度。我们首先将根节点和深度0入栈。然后,我们不断从栈中取出节点,如果该节点有左子树或右子树,则将它们和对应的深度入栈。我们不断重复这个过程,直到栈为空。最后,我们返回最大深度。 ```java import java.util.Stack; public class Solution { public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } //非递归版本 public int TreeDepth(TreeNode root) { if(root==null) return 0; Stack<TreeNode> stack=new Stack<>(); Stack<Integer> depth=new Stack<>(); stack.push(root); depth.push(1); int maxDepth=0; while(!stack.isEmpty()){ TreeNode node=stack.pop(); int d=depth.pop(); maxDepth=Math.max(maxDepth,d); if(node.left!=null){ stack.push(node.left); depth.push(d+1); } if(node.right!=null){ stack.push(node.right); depth.push(d+1); } } return maxDepth; } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值