当前位置:网站首页>Leetcode-104. Maximum Depth of Binary Tree

Leetcode-104. Maximum Depth of Binary Tree

2022-06-11 07:06:00 Eistert

subject

Given the root of a binary tree, return its maximum depth.

A binary tree’s maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

Example 1:
 Insert picture description here

Input: root = [3,9,20,null,null,15,7]
Output: 3

Example 2:

Input: root = [1,null,2]
Output: 2

Constraints:

  • The number of nodes in the tree is in the range [0, 104].
  • -100 <= Node.val <= 100

solution

Method 1 : Depth-first search

Code

    /** * Definition for a binary tree node. * public 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; * } * } */
    class Solution {
    
        public int maxDepth(TreeNode root) {
    
            if(root == null){
    
                return 0;
            }

            return 1 + Math.max(maxDepth(root.left),maxDepth(root.right));

        }
    }

Complexity analysis

  • Time complexity :O(n);
  • Spatial complexity :O(height);

Method 2 : Breadth first search

We can also use 【 Breadth first search 】 To solve this problem , But we need to make some changes , At this point, our breadth first search queue is 【 All nodes of the current layer 】. Every time you expand to the next level , Unlike breadth first search, only one node is taken out of the queue at a time , We need to expand all nodes in the queue , This ensures that all nodes of the current layer are stored in the queue every time the expansion is completed , That is, we expand layer by layer , Finally, we use a variable ans To maintain the number of expansions , The maximum depth of the binary tree is ans.

Code

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

            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            //  Add the root node to the queue 
            queue.offer(root);

            //  The initial result is 0
            int ans = 0;
            while (!queue.isEmpty()) {
    
                //  The number of nodes in the current layer 
                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--;
                }
                //  The number of layers plus 1
                ans++;
            }
            return ans;
        }

Complexity analysis

  • Time complexity :O(n);
  • Spatial complexity :O(height);

Reprint

author :LeetCode-Solution
link :https://leetcode.cn/problems/maximum-depth-of-binary-tree/solution/er-cha-shu-de-zui-da-shen-du-by-leetcode-solution/
source : Power button (LeetCode)
The copyright belongs to the author . Commercial reprint please contact the author for authorization , Non-commercial reprint please indicate the source .

原网站

版权声明
本文为[Eistert]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206110656270861.html