File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change 432
432
1443|[ Minimum Time to Collect All Apples in a Tree] ( ./1443-minimum-time-to-collect-all-apples-in-a-tree.js ) |Medium|
433
433
1446|[ Consecutive Characters] ( ./1446-consecutive-characters.js ) |Easy|
434
434
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|
435
436
1450|[ Number of Students Doing Homework at a Given Time] ( ./1450-number-of-students-doing-homework-at-a-given-time.js ) |Easy|
436
437
1451|[ Rearrange Words in a Sentence] ( ./1451-rearrange-words-in-a-sentence.js ) |Medium|
437
438
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|
Original file line number Diff line number Diff line change
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
+ } ;
You can’t perform that action at this time.
0 commit comments