列表

详情


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

给你一棵根为 root 的二叉树,请你返回二叉树中好节点的数目。

「好节点」X 定义为:从根到该节点 X 所经过的节点中,没有任何节点的值大于 X 的值。

 

示例 1:

输入:root = [3,1,4,3,null,1,5]
输出:4
解释:图中蓝色节点为好节点。
根节点 (3) 永远是个好节点。
节点 4 -> (3,4) 是路径中的最大值。
节点 5 -> (3,4,5) 是路径中的最大值。
节点 3 -> (3,1,3) 是路径中的最大值。

示例 2:

输入:root = [3,3,null,4,2]
输出:3
解释:节点 2 -> (3, 3, 2) 不是好节点,因为 "3" 比它大。

示例 3:

输入:root = [1]
输出:1
解释:根节点是好节点。

 

提示:

原站题解

去查看

上次编辑到这里,代码来自缓存 点击恢复默认模板
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode() : val(0), left(nullptr), right(nullptr) {} * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: int goodNodes(TreeNode* root) { } };

php 解法, 执行用时: 72 ms, 内存消耗: 33.6 MB, 提交时间: 2023-08-25 07:41:48

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     public $val = null;
 *     public $left = null;
 *     public $right = null;
 *     function __construct($val = 0, $left = null, $right = null) {
 *         $this->val = $val;
 *         $this->left = $left;
 *         $this->right = $right;
 *     }
 * }
 */
class Solution {

    /**
     * @param TreeNode $root
     * @return Integer
     */
    function goodNodes($root) {
        return $this->dfs($root, PHP_INT_MIN);
    }
    
    function dfs($root, $path_max) {
        if ( $root == null ) return 0;
        $res = 0;
        if ( $root->val >= $path_max ) {
            $res++;
            $path_max = $root->val;
        }
        $res += $this->dfs($root->left, $path_max) + $this->dfs($root->right, $path_max);
        return $res;
    }
}

javascript 解法, 执行用时: 124 ms, 内存消耗: 63.5 MB, 提交时间: 2023-08-25 07:37:28

/**
 * 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) {
    const dfs = (root, path_max) => {
        if (root == null) {
            return 0;
        }
        let res = 0;
        if (root.val >= path_max) {
            res++;
            path_max = root.val;
        }
        res += dfs(root.left, path_max) + dfs(root.right, path_max);
        return res;
    }
    return dfs(root, -Infinity);
};

golang 解法, 执行用时: 80 ms, 内存消耗: 18.4 MB, 提交时间: 2023-08-25 07:36:22

/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func goodNodes(root *TreeNode) int {
    return dfs(root, math.MinInt)
}

func dfs(root *TreeNode, path_max int) int {
    if root == nil {
        return 0
    }
    res := 0
    if root.Val >= path_max {
        res++
        path_max = root.Val
    }
    res += dfs(root.Left, path_max) + dfs(root.Right, path_max)
    return res
}

cpp 解法, 执行用时: 124 ms, 内存消耗: 84.3 MB, 提交时间: 2022-11-19 20:15:40

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    int ans;
    void dfs(TreeNode *root,int mymax)
    {
        if(root==nullptr)
            return ;
        if(root->val>=mymax)
            ans++;
        dfs(root->left,max(mymax,root->val));
        dfs(root->right,max(mymax,root->val));
    }
    int goodNodes(TreeNode* root) 
    {
        dfs(root,-2147483648);
        return ans;
    }
};

java 解法, 执行用时: 2 ms, 内存消耗: 49.5 MB, 提交时间: 2022-11-19 20:14:20

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int goodNodes(TreeNode root) {
        if (root == null) return 0;
        dfs(root, root.val);
        return ans;
    }

    int ans = 0;
    void dfs(TreeNode node, int curMax) {
        if (node == null) return;
        if (node.val >= curMax) {
            ans++;
            curMax = node.val;
        }
        dfs(node.left, curMax);
        dfs(node.right, curMax);
    }
}

java 解法, 执行用时: 2 ms, 内存消耗: 49.5 MB, 提交时间: 2022-11-19 20:14:02

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
	public int goodNodes(TreeNode root) {
		return this.goodNodes(root, root.val);
	}

	private int goodNodes(TreeNode root, int max) {
		if (root == null) {
			return 0;
		}
		int ans = root.val >= max ? 1 : 0;
		max = Math.max(max, root.val);
		ans += this.goodNodes(root.left, max);
		ans += this.goodNodes(root.right, max);
		return ans;
	}
}

python3 解法, 执行用时: 236 ms, 内存消耗: 33.3 MB, 提交时间: 2022-11-19 20:13:24

# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def goodNodes(self, root: TreeNode) -> int:
        ans = 1
        
        def dfs(u: TreeNode, Max: int) -> None:
            nonlocal ans
            if u.val >= Max:
                ans += 1
            if u.left:
                dfs(u.left, max(Max, u.val))
            if u.right:
                dfs(u.right, max(Max, u.val))
        
        if root.left:
            dfs(root.left, root.val)
        if root.right:
            dfs(root.right, root.val)
        return ans

上一题