一、手写算法

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

思路


  • 代码

    1. /**
    2. * Definition for a binary tree node.
    3. * function TreeNode(val, left, right) {
    4. * this.val = (val===undefined ? 0 : val)
    5. * this.left = (left===undefined ? null : left)
    6. * this.right = (right===undefined ? null : right)
    7. * }
    8. */
    9. /**
    10. * @param {TreeNode} root
    11. * @return {number}
    12. */
    13. var maxDepth = function(root) {
    14. if(!root) {
    15. return 0;
    16. } else {
    17. const left = maxDepth(root.left);
    18. const right = maxDepth(root.right);
    19. return Math.max(left, right) + 1;
    20. }
    21. };

    复杂度分析

  • 时间复杂度:O(N)

  • 空间复杂度:O(N)

二、编程题

//2.手写题:https://bigfrontend.dev/zh/problem/immerjs