-
Notifications
You must be signed in to change notification settings - Fork 0
/
No_1448.cs
39 lines (34 loc) · 986 Bytes
/
No_1448.cs
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
32
33
34
35
36
37
38
39
namespace LeetCode
{
class No_1448
{
public int GoodNodes(TreeNode root)
{
if (root.left == null && root.right == null)
return 1;
return Dfs(root, 1);
}
public int Dfs(TreeNode root, int cont)
{
if (root.left != null)
{
if (root.left.val >= root.val)
cont++;
else
root.left.val = root.val;
if (root.left.left != null || root.left.right != null)
cont = Dfs(root.left, cont);
}
if (root.right != null)
{
if (root.right.val >= root.val)
cont++;
else
root.right.val = root.val;
if (root.right.left != null || root.right.right != null)
cont = Dfs(root.right, cont);
}
return cont;
}
}
}