Appearance
104二叉树最大深度
code
javascript
/**
* 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) {
let result = 0;
function traverse(root, depth){
if(root === null){
return;
}
result = Math.max(result, depth);
traverse(root.left, (depth + 1));
traverse(root.right, (depth + 1));
}
traverse(root, 1);
return result;
};总结
- 说来好像也没啥,类似层序遍历那种的,记录下长期状态
- 我觉得没啥问题