当前位置:   article > 正文

【Hot100】LeetCode—104. 二叉树的最大深度

【Hot100】LeetCode—104. 二叉树的最大深度


1- 思路

队列——层序遍历

  • 1- 借助队列实现层序遍历 Queue<TreeNode> queue = new LinkedList<>()
  • 2- 遍历条件
    • while(!queue.isEmpty())

2- 实现

⭐104. 二叉树的最大深度——题解思路

在这里插入图片描述

class Solution {
    List<Integer> res = new ArrayList<>();
    public int maxDepth(TreeNode root) {
        int depth = 0;
        if(root==null){
            return depth;
        }
        // 借助队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            // path
            while(len>0){
                TreeNode nowNode = queue.poll();
                if(nowNode.left != null) queue.offer(nowNode.left);
                if(nowNode.right != null) queue.offer(nowNode.right);
                len--;
            }
            depth++;
        }
        return depth;
    }
}

3- ACM 实现

public class maxDepth {
    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;
        TreeNode() {}
        TreeNode(int val) { this.val = val; }
        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
    public static TreeNode build(String str){
        if(str == null || str.length()==0){
            return null;
        }
        String input = str.replace("[","");
        input = input.replace("]","");
        String[] parts = input.split(",");

        Integer[] nums = new Integer[parts.length];
        for(int i = 0 ; i < parts.length;i++){
            if(!parts[i].equals("null")){
                nums[i] = Integer.parseInt(parts[i]);
            }else{
                nums[i] = null;
            }
        }
        Queue<TreeNode> queue = new LinkedList<>();
        TreeNode root = new TreeNode(nums[0]);
        queue.offer(root);
        int index = 1;
        while(!queue.isEmpty()&& index<parts.length){
            TreeNode node = queue.poll();
            if(index<nums.length && nums[index]!=null){
                node.left = new TreeNode(nums[index]);
                queue.offer(node.left);
            }
            index++;
            if(index<nums.length && nums[index]!=null){
                node.right = new TreeNode(nums[index]);
                queue.offer(node.right);
            }
            index++;
        }
        return root;
    }

    public static int maxDepth(TreeNode root) {
        int depth = 0;
        if(root==null){
            return depth;
        }
        // 借助队列
        Queue<TreeNode> queue = new LinkedList<>();
        queue.offer(root);
        while(!queue.isEmpty()){
            int len = queue.size();
            // path
            while(len>0){
                TreeNode nowNode = queue.poll();
                if(nowNode.left != null) queue.offer(nowNode.left);
                if(nowNode.right != null) queue.offer(nowNode.right);
                len--;
            }
            depth++;
        }
        return depth;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String input = sc.nextLine();
        TreeNode root = build(input);
        System.out.println("结果是"+maxDepth(root));
    }
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/1021275
推荐阅读
相关标签
  

闽ICP备14008679号