Skip to content

Commit 5d514fd

Browse files
committed
Add solution #1448
1 parent b748374 commit 5d514fd

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@
432432
1443|[Minimum Time to Collect All Apples in a Tree](./1443-minimum-time-to-collect-all-apples-in-a-tree.js)|Medium|
433433
1446|[Consecutive Characters](./1446-consecutive-characters.js)|Easy|
434434
1447|[Simplified Fractions](./1447-simplified-fractions.js)|Medium|
435+
1448|[Count Good Nodes in Binary Tree](./1448-count-good-nodes-in-binary-tree.js)|Medium|
435436
1450|[Number of Students Doing Homework at a Given Time](./1450-number-of-students-doing-homework-at-a-given-time.js)|Easy|
436437
1451|[Rearrange Words in a Sentence](./1451-rearrange-words-in-a-sentence.js)|Medium|
437438
1455|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](./1455-check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence.js)|Easy|
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* 1448. Count Good Nodes in Binary Tree
3+
* https://leetcode.com/problems/count-good-nodes-in-binary-tree/
4+
* Difficulty: Medium
5+
*
6+
* Given a binary tree root, a node X in the tree is named good if in the path from
7+
* root to X there are no nodes with a value greater than X.
8+
*
9+
* Return the number of good nodes in the binary tree.
10+
*/
11+
12+
/**
13+
* Definition for a binary tree node.
14+
* function TreeNode(val, left, right) {
15+
* this.val = (val===undefined ? 0 : val)
16+
* this.left = (left===undefined ? null : left)
17+
* this.right = (right===undefined ? null : right)
18+
* }
19+
*/
20+
/**
21+
* @param {TreeNode} root
22+
* @return {number}
23+
*/
24+
var goodNodes = function(root, max = -Infinity) {
25+
if (!root) return 0;
26+
const n = root.val >= max ? 1 : 0;
27+
return n + goodNodes(root.left, n ? root.val : max) + goodNodes(root.right, n ? root.val : max);
28+
};

0 commit comments

Comments
 (0)