Skip to content

Commit f8d6aa1

Browse files
committed
feat: add solutions to lc problem: No.1448
No.1448.Count Good Nodes in Binary Tree
1 parent 58a5daf commit f8d6aa1

File tree

6 files changed

+362
-2
lines changed

6 files changed

+362
-2
lines changed

solution/1400-1499/1448.Count Good Nodes in Binary Tree/README.md

+125-1
Original file line numberDiff line numberDiff line change
@@ -51,22 +51,146 @@
5151

5252
<!-- 这里可写通用的实现逻辑 -->
5353

54+
DFS,利用 mx 变量记录已经访问过的节点的最大值,与当前将要访问的节点 root 比较大小。
55+
5456
<!-- tabs:start -->
5557

5658
### **Python3**
5759

5860
<!-- 这里可写当前语言的特殊实现逻辑 -->
5961

6062
```python
61-
63+
# Definition for a binary tree node.
64+
# class TreeNode:
65+
# def __init__(self, val=0, left=None, right=None):
66+
# self.val = val
67+
# self.left = left
68+
# self.right = right
69+
class Solution:
70+
def goodNodes(self, root: TreeNode) -> int:
71+
def dfs(root, mx):
72+
if root is None:
73+
return
74+
nonlocal ans
75+
if mx <= root.val:
76+
ans += 1
77+
mx = root.val
78+
dfs(root.left, mx)
79+
dfs(root.right, mx)
80+
81+
ans = 0
82+
dfs(root, -10000)
83+
return ans
6284
```
6385

6486
### **Java**
6587

6688
<!-- 这里可写当前语言的特殊实现逻辑 -->
6789

6890
```java
91+
/**
92+
* Definition for a binary tree node.
93+
* public class TreeNode {
94+
* int val;
95+
* TreeNode left;
96+
* TreeNode right;
97+
* TreeNode() {}
98+
* TreeNode(int val) { this.val = val; }
99+
* TreeNode(int val, TreeNode left, TreeNode right) {
100+
* this.val = val;
101+
* this.left = left;
102+
* this.right = right;
103+
* }
104+
* }
105+
*/
106+
class Solution {
107+
private int ans;
108+
109+
public int goodNodes(TreeNode root) {
110+
ans = 0;
111+
dfs(root, -10000);
112+
return ans;
113+
}
114+
115+
private void dfs(TreeNode root, int mx) {
116+
if (root == null) {
117+
return;
118+
}
119+
if (mx <= root.val) {
120+
++ans;
121+
mx = root.val;
122+
}
123+
dfs(root.left, mx);
124+
dfs(root.right, mx);
125+
}
126+
}
127+
```
128+
129+
### **C++**
130+
131+
```cpp
132+
/**
133+
* Definition for a binary tree node.
134+
* struct TreeNode {
135+
* int val;
136+
* TreeNode *left;
137+
* TreeNode *right;
138+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
139+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
140+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
141+
* };
142+
*/
143+
class Solution {
144+
public:
145+
int ans;
146+
147+
int goodNodes(TreeNode* root) {
148+
ans = 0;
149+
dfs(root, -10000);
150+
return ans;
151+
}
152+
153+
void dfs(TreeNode* root, int mx) {
154+
if (!root) return;
155+
if (mx <= root->val)
156+
{
157+
++ans;
158+
mx = root->val;
159+
}
160+
dfs(root->left, mx);
161+
dfs(root->right, mx);
162+
}
163+
};
164+
```
69165
166+
### **Go**
167+
168+
```go
169+
/**
170+
* Definition for a binary tree node.
171+
* type TreeNode struct {
172+
* Val int
173+
* Left *TreeNode
174+
* Right *TreeNode
175+
* }
176+
*/
177+
func goodNodes(root *TreeNode) int {
178+
ans := 0
179+
var dfs func(root *TreeNode, mx int)
180+
dfs = func(root *TreeNode, mx int) {
181+
if root == nil {
182+
return
183+
}
184+
if mx <= root.Val {
185+
ans++
186+
mx = root.Val
187+
}
188+
dfs(root.Left, mx)
189+
dfs(root.Right, mx)
190+
}
191+
dfs(root, -10000)
192+
return ans
193+
}
70194
```
71195

72196
### **...**

solution/1400-1499/1448.Count Good Nodes in Binary Tree/README_EN.md

+123-1
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,135 @@ Node 3 -&gt; (3,1,3) is the maximum value in the path.</pre>
6868
### **Python3**
6969

7070
```python
71-
71+
# Definition for a binary tree node.
72+
# class TreeNode:
73+
# def __init__(self, val=0, left=None, right=None):
74+
# self.val = val
75+
# self.left = left
76+
# self.right = right
77+
class Solution:
78+
def goodNodes(self, root: TreeNode) -> int:
79+
def dfs(root, mx):
80+
if root is None:
81+
return
82+
nonlocal ans
83+
if mx <= root.val:
84+
ans += 1
85+
mx = root.val
86+
dfs(root.left, mx)
87+
dfs(root.right, mx)
88+
89+
ans = 0
90+
dfs(root, -10000)
91+
return ans
7292
```
7393

7494
### **Java**
7595

7696
```java
97+
/**
98+
* Definition for a binary tree node.
99+
* public class TreeNode {
100+
* int val;
101+
* TreeNode left;
102+
* TreeNode right;
103+
* TreeNode() {}
104+
* TreeNode(int val) { this.val = val; }
105+
* TreeNode(int val, TreeNode left, TreeNode right) {
106+
* this.val = val;
107+
* this.left = left;
108+
* this.right = right;
109+
* }
110+
* }
111+
*/
112+
class Solution {
113+
private int ans;
114+
115+
public int goodNodes(TreeNode root) {
116+
ans = 0;
117+
dfs(root, -10000);
118+
return ans;
119+
}
120+
121+
private void dfs(TreeNode root, int mx) {
122+
if (root == null) {
123+
return;
124+
}
125+
if (mx <= root.val) {
126+
++ans;
127+
mx = root.val;
128+
}
129+
dfs(root.left, mx);
130+
dfs(root.right, mx);
131+
}
132+
}
133+
```
134+
135+
### **C++**
136+
137+
```cpp
138+
/**
139+
* Definition for a binary tree node.
140+
* struct TreeNode {
141+
* int val;
142+
* TreeNode *left;
143+
* TreeNode *right;
144+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
145+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
146+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
147+
* };
148+
*/
149+
class Solution {
150+
public:
151+
int ans;
152+
153+
int goodNodes(TreeNode* root) {
154+
ans = 0;
155+
dfs(root, -10000);
156+
return ans;
157+
}
158+
159+
void dfs(TreeNode* root, int mx) {
160+
if (!root) return;
161+
if (mx <= root->val)
162+
{
163+
++ans;
164+
mx = root->val;
165+
}
166+
dfs(root->left, mx);
167+
dfs(root->right, mx);
168+
}
169+
};
170+
```
77171
172+
### **Go**
173+
174+
```go
175+
/**
176+
* Definition for a binary tree node.
177+
* type TreeNode struct {
178+
* Val int
179+
* Left *TreeNode
180+
* Right *TreeNode
181+
* }
182+
*/
183+
func goodNodes(root *TreeNode) int {
184+
ans := 0
185+
var dfs func(root *TreeNode, mx int)
186+
dfs = func(root *TreeNode, mx int) {
187+
if root == nil {
188+
return
189+
}
190+
if mx <= root.Val {
191+
ans++
192+
mx = root.Val
193+
}
194+
dfs(root.Left, mx)
195+
dfs(root.Right, mx)
196+
}
197+
dfs(root, -10000)
198+
return ans
199+
}
78200
```
79201

80202
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* struct TreeNode {
4+
* int val;
5+
* TreeNode *left;
6+
* TreeNode *right;
7+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
8+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
9+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10+
* };
11+
*/
12+
class Solution {
13+
public:
14+
int ans;
15+
16+
int goodNodes(TreeNode* root) {
17+
ans = 0;
18+
dfs(root, -10000);
19+
return ans;
20+
}
21+
22+
void dfs(TreeNode* root, int mx) {
23+
if (!root) return;
24+
if (mx <= root->val)
25+
{
26+
++ans;
27+
mx = root->val;
28+
}
29+
dfs(root->left, mx);
30+
dfs(root->right, mx);
31+
}
32+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* type TreeNode struct {
4+
* Val int
5+
* Left *TreeNode
6+
* Right *TreeNode
7+
* }
8+
*/
9+
func goodNodes(root *TreeNode) int {
10+
ans := 0
11+
var dfs func(root *TreeNode, mx int)
12+
dfs = func(root *TreeNode, mx int) {
13+
if root == nil {
14+
return
15+
}
16+
if mx <= root.Val {
17+
ans++
18+
mx = root.Val
19+
}
20+
dfs(root.Left, mx)
21+
dfs(root.Right, mx)
22+
}
23+
dfs(root, -10000)
24+
return ans
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode() {}
8+
* TreeNode(int val) { this.val = val; }
9+
* TreeNode(int val, TreeNode left, TreeNode right) {
10+
* this.val = val;
11+
* this.left = left;
12+
* this.right = right;
13+
* }
14+
* }
15+
*/
16+
class Solution {
17+
private int ans;
18+
19+
public int goodNodes(TreeNode root) {
20+
ans = 0;
21+
dfs(root, -10000);
22+
return ans;
23+
}
24+
25+
private void dfs(TreeNode root, int mx) {
26+
if (root == null) {
27+
return;
28+
}
29+
if (mx <= root.val) {
30+
++ans;
31+
mx = root.val;
32+
}
33+
dfs(root.left, mx);
34+
dfs(root.right, mx);
35+
}
36+
}

0 commit comments

Comments
 (0)