Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit00d015e

Browse files
committed
Create: 1448-Count-Good-Nodes-in-Binary-Tree.ts
1 parent92d54b8 commit00d015e

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
functiongoodNodes(root:TreeNode|null):number{
2+
// edge case
3+
if(root===null)return0;
4+
5+
//keep track of the count
6+
letcount=0;
7+
8+
constdfs=(root,max)=>{
9+
//if value is bigger than max of path, then it is a good node
10+
if(root.val>=max)count++;
11+
12+
//extend search to children if not null with updated max(takes current node in consideration too)
13+
if(root.left!==null)dfs(root.left,Math.max(max,root.val));
14+
if(root.right!==null)dfs(root.right,Math.max(max,root.val));
15+
};
16+
17+
//starts with negative max value, because min value is just the smaller positive value in javascript
18+
dfs(root,-Number.MAX_VALUE);
19+
20+
returncount;
21+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp