统计二叉树中好节点的数目

统计二叉树中好节点的数目

#resource / algorithm #type / snippet #status / evergreen #source / leetcode #ds / tree #algo / dfs

[!info] related notes 算法面试题型 MOC 二叉树高频题

统计二叉树中好节点的数目

题目

1448. 统计二叉树中好节点的数目 - 力扣(LeetCode)

题解

链表-二叉树-回溯

var goodNodes = function(root) {
    let res = 0;
    if (!root) return 0;
    // 把 当前路线上的最大值传下去
    function dfs(node, max) {
        if (!node) return;
        if (node.val >= max) {
            res++;
        }
        max = Math.max(max, node.val);
        dfs(node.left,max);
        dfs(node.right,max)
    }
    dfs(root, -Infinity)
    return res;
};

参考

创建于 2025/1/1 更新于 2026/5/27