Skip to content

Commit 78b0f1a

Browse files
authored
Create 1448-count-good-nodes-in-binary-tree.js
1 parent 14fdbd2 commit 78b0f1a

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
10+
/**
11+
* @param {TreeNode} root
12+
* @return {number}
13+
*/
14+
const goodNodes = function(root) {
15+
if(root == null) return 0
16+
let res = 0
17+
18+
helper(root, root.val)
19+
20+
return res
21+
22+
function helper(node, max) {
23+
if(node == null) return
24+
if(node.val >= max) {
25+
res++
26+
max = node.val
27+
}
28+
helper(node.left, max)
29+
helper(node.right, max)
30+
}
31+
};

0 commit comments

Comments
 (0)