-
Notifications
You must be signed in to change notification settings - Fork 0
/
35-1448.ts
31 lines (26 loc) · 895 Bytes
/
35-1448.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// https://leetcode.com/problems/path-sum-iii/description/?envType=study-plan-v2&envId=leetcode-75
/**
* Definition for a binary tree node.
* class TreeNode {
* val: number
* left: TreeNode | null
* right: TreeNode | null
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
* }
*/
// @ts-nocheck
const depthFirstGoodCount = (root: TreeNode | null, max: number): number => {
if (root === null) return 0;
max = Math.max(root.val, max);
return (
(root.val === max ? 1 : 0) +
depthFirstGoodCount(root.left, max) +
depthFirstGoodCount(root.right, max)
);
};
const goodNodes = (root: TreeNode | null): number =>
depthFirstGoodCount(root, root.val);