【104】二叉树的最大深度有两种解法。递归比较容易想到,迭代需要重新思考?1.递归函数maxDepth(root){if(!root){return0;}else{letleft=maxDepth(root.left);让right=maxDepth(root.right);返回Math.max(左,右)+1;}}2.迭代,使用层序遍历累加constmaxDepth=root=>{if(!root)return0letqueue=[root],n=0console.log('queue',queue.length)while(queue.length){letarr=[]while(queue.length){letcurr=queue.shift()console.log('curr',curr)console.log('arr',arr)if(curr.left)arr.push(curr.left)如果(curr.right)arr.push(curr.right)}n++queue=arr}returnn}[122]买卖股票的最佳时机2两种解决方法:第一种是自己搞的笨方法,第二种是参考别人的思路1、/***@param{number[]}prices*@return{number}*/varmaxProfit=function(prices){if(prices.length<2)return0;让结果=0;让small=prices[0]for(vari=0;i
