Appearance
1448二叉树好节点
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 goodNodes = function(root) {
let result = 0;
function helper(root, max){
if(root === null){
return;
}
if(root.val >= max){
result++;
helper(root.left, root.val);
helper(root.right, root.val);
} else {
helper(root.left, max);
helper(root.right, max);
}
}
helper(root, -Infinity);
return result;
};总结
- 没啥特殊的感觉,跟层序遍历似乎是差不多的东西
- 感觉不用看啥,一个值得注意的事情是根节点0的问题,老默认标成0忘带负数