【力扣算法】104-二叉树的最大深度

题目

给定一个二叉树,找出其最大深度。

二叉树的深度为根节点到最远叶子节点的最长路径上的节点数。

说明: 叶子节点是指没有子节点的节点。

示例:
给定二叉树 [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

返回它的最大深度 3 。

来源:力扣(LeetCode)
链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

题解

树的定义

首先,给出我们将要使用的树的结点 TreeNode 的定义。

  /* Definition for a binary tree node. */
  public class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;

    TreeNode(int x) {
      val = x;
    }
  }

方法一:递归

算法

直观的方法是通过递归来解决问题。在这里,我们演示了 DFS(深度优先搜索)策略的示例。

class Solution {
  public int maxDepth(TreeNode root) {
    if (root == null) {
      return 0;
    } else {
      int left_height = maxDepth(root.left);
      int right_height = maxDepth(root.right);
      return java.lang.Math.max(left_height, right_height) + 1;
    }
  }
}

复杂度分析

  • 时间复杂度:我们每个结点只访问一次,因此时间复杂度为 O ( N ) O(N) O(N), 其中 N N N 是结点的数量。
  • 空间复杂度:在最糟糕的情况下,树是完全不平衡的,例如每个结点只剩下左子结点,递归将会被调用 N N N 次(树的高度),因此保持调用栈的存储将是 O ( N ) O(N) O(N)。但在最好的情况下(树是完全平衡的),树的高度将是 log ⁡ ( N ) \log(N) log(N)。因此,在这种情况下的空间复杂度将是 O ( log ⁡ ( N ) ) O(\log(N)) O(log(N))

方法二:迭代

我们还可以在栈的帮助下将上面的递归转换为迭代。

我们的想法是使用 DFS 策略访问每个结点,同时在每次访问时更新最大深度。

所以我们从包含根结点且相应深度为 1 的栈开始。然后我们继续迭代:将当前结点弹出栈并推入子结点。每一步都会更新深度。

import javafx.util.Pair;
import java.lang.Math;

class Solution {
  public int maxDepth(TreeNode root) {
    Queue<Pair<TreeNode, Integer>> stack = new LinkedList<>();
    if (root != null) {
      stack.add(new Pair(root, 1));
    }

    int depth = 0;
    while (!stack.isEmpty()) {
      Pair<TreeNode, Integer> current = stack.poll();
      root = current.getKey();
      int current_depth = current.getValue();
      if (root != null) {
        depth = Math.max(depth, current_depth);
        stack.add(new Pair(root.left, current_depth + 1));
        stack.add(new Pair(root.right, current_depth + 1));
      }
    }
    return depth;
  }
};

复杂度分析

时间复杂度: O ( N ) O(N) O(N)
空间复杂度: O ( N ) O(N) O(N)

作者:LeetCode
链接:https://leetcode-cn.com/problems/two-sum/solution/er-cha-shu-de-zui-da-shen-du-by-leetcode/
来源:力扣(LeetCode)
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

感想

继续100、101、102、103的祖传代码…… 其实用递归还更简单的……

执行用时 :2 ms, 在所有 Java 提交中击败了16.73%的用户

内存消耗 :36.6 MB, 在所有 Java 提交中击败了64.70%的用户

/**
* Definition for a binary tree node.
* public class TreeNode {
*     int val;
*     TreeNode left;
*     TreeNode right;
*     TreeNode(int x) { val = x; }
* }
*/
class Solution {
  public int maxDepth(TreeNode root) {
      int res = 1;
      if(root==null) return 0;
      LinkedList<TreeNode> queue = new LinkedList<>();
      queue.add(root);
      TreeNode nextFirst = null;
      TreeNode temp = null;
      while(!queue.isEmpty()){
          temp = queue.poll();
          if(temp==nextFirst) {
              nextFirst=null;
              res++;
          }
          if(temp.left!=null){
              queue.add(temp.left);
              if(nextFirst==null) nextFirst = temp.left;
          }
          if(temp.right!=null){
              queue.add(temp.right);
              if(nextFirst==null) nextFirst = temp.right;
          }
      }
      return res;
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值