zoukankan      html  css  js  c++  java
  • 【LeetCode】104.二叉树的最大深度(递归+迭代,java实现)

    题目

    链接:https://leetcode-cn.com/problems/maximum-depth-of-binary-tree/

    image-20200627133012121

    分析

    树的定义

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

      /* 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) {
        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),
      其中 N 是结点的数量。
    • 空间复杂度:在最糟糕的情况下,树是完全不平衡的,例如每个结点只剩下左子结点,递归将会被调用 N次(树的高度),因此保持调用栈的存储将是 O(N)。但在最好的情况下(树是完全平衡的),树的高度将是 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)。
  • 相关阅读:
    陈应松《母亲》
    黄灯:一个农村儿媳眼中的乡村图景
    喝完茶为什么嘴里是甜的
    俗语一千条
    XtraBackup完整备份与增量备份的原理
    李嘉诚:90%考虑失败 关注细节
    redo和undo的区别
    tar命令
    自增锁引发的悲剧
    各版本 MySQL 并行复制的实现及优缺点
  • 原文地址:https://www.cnblogs.com/hzcya1995/p/13308021.html
Copyright © 2011-2022 走看看