一、手写算法
https://leetcode-cn.com/problems/maximum-depth-of-binary-tree
思路
-
代码
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {number}
*/
var maxDepth = function(root) {
if(!root) {
return 0;
} else {
const left = maxDepth(root.left);
const right = maxDepth(root.right);
return Math.max(left, right) + 1;
}
};
复杂度分析
时间复杂度:O(N)
- 空间复杂度:O(N)
二、编程题
//2.手写题:https://bigfrontend.dev/zh/problem/immerjs