Skip to content

Commit 3992394

Browse files
iamAntimPalAntim-IWPIamShiwangi
committed
Update readme.md
Co-Authored-By: Antim-IWP <203163676+Antim-IWP@users.noreply.github.com> Co-Authored-By: Shiwangi Srivastava <174641070+IamShiwangi@users.noreply.github.com>
1 parent 51cb612 commit 3992394

File tree

1 file changed

+225
-0
lines changed
  • Solution/1448. Count Good Nodes in Binary Tree

1 file changed

+225
-0
lines changed
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
Sure! Here's the full `README.md` file for **LeetCode Problem 1448. Count Good Nodes in Binary Tree**, following the exact style you requested, like the `doocs/leetcode` GitHub repo:
2+
3+
---
4+
5+
```markdown
6+
# [1448. Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree)
7+
8+
> Difficulty: Medium
9+
> Biweekly Contest 26 Q3
10+
> Tags: Tree, Depth-First Search, Breadth-First Search, Binary Tree
11+
12+
## Description
13+
14+
Given a binary tree `root`, a node **X** in the tree is named **good** if in the path from root to **X** there are no nodes with a value **greater than** X.
15+
16+
Return the number of **good** nodes in the binary tree.
17+
18+
### Example 1:
19+
20+
![example1](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1448.Count%20Good%20Nodes%20in%20Binary%20Tree/images/test_sample_1.png)
21+
22+
```
23+
Input: root = [3,1,4,3,null,1,5]
24+
Output: 4
25+
Explanation: Nodes in blue are good.
26+
Root Node (3) is always a good node.
27+
Node 4 -> (3,4) is the maximum value in the path starting from the root.
28+
Node 5 -> (3,4,5) is the maximum value in the path
29+
Node 3 -> (3,1,3) is the maximum value in the path.
30+
```
31+
32+
### Example 2:
33+
34+
![example2](https://fastly.jsdelivr.net/gh/doocs/leetcode@main/solution/1400-1499/1448.Count%20Good%20Nodes%20in%20Binary%20Tree/images/test_sample_2.png)
35+
36+
```
37+
Input: root = [3,3,null,4,2]
38+
Output: 3
39+
Explanation: Node 2 -> (3, 3, 2) is not good, because "3" is higher than it.
40+
```
41+
42+
### Example 3:
43+
44+
```
45+
Input: root = [1]
46+
Output: 1
47+
Explanation: Root is considered as good.
48+
```
49+
50+
### Constraints:
51+
52+
- The number of nodes in the binary tree is in the range `[1, 10^5]`.
53+
- Each node's value is between `[-10^4, 10^4]`.
54+
55+
## Solutions
56+
57+
### Python3
58+
59+
```python
60+
# Definition for a binary tree node.
61+
# class TreeNode:
62+
# def __init__(self, val=0, left=None, right=None):
63+
# self.val = val
64+
# self.left = left
65+
# self.right = right
66+
class Solution:
67+
def goodNodes(self, root: TreeNode) -> int:
68+
def dfs(root: TreeNode, mx: int):
69+
if root is None:
70+
return
71+
nonlocal ans
72+
if mx <= root.val:
73+
ans += 1
74+
mx = root.val
75+
dfs(root.left, mx)
76+
dfs(root.right, mx)
77+
78+
ans = 0
79+
dfs(root, -1000000)
80+
return ans
81+
```
82+
83+
### Java
84+
85+
```java
86+
/**
87+
* Definition for a binary tree node.
88+
* public class TreeNode {
89+
* int val;
90+
* TreeNode left;
91+
* TreeNode right;
92+
* TreeNode() {}
93+
* TreeNode(int val) { this.val = val; }
94+
* TreeNode(int val, TreeNode left, TreeNode right) {
95+
* this.val = val;
96+
* this.left = left;
97+
* this.right = right;
98+
* }
99+
* }
100+
*/
101+
class Solution {
102+
private int ans = 0;
103+
104+
public int goodNodes(TreeNode root) {
105+
dfs(root, -100000);
106+
return ans;
107+
}
108+
109+
private void dfs(TreeNode root, int mx) {
110+
if (root == null) {
111+
return;
112+
}
113+
if (mx <= root.val) {
114+
++ans;
115+
mx = root.val;
116+
}
117+
dfs(root.left, mx);
118+
dfs(root.right, mx);
119+
}
120+
}
121+
```
122+
123+
### C++
124+
125+
```cpp
126+
/**
127+
* Definition for a binary tree node.
128+
* struct TreeNode {
129+
* int val;
130+
* TreeNode *left;
131+
* TreeNode *right;
132+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
133+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
134+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
135+
* };
136+
*/
137+
class Solution {
138+
public:
139+
int goodNodes(TreeNode* root) {
140+
int ans = 0;
141+
function<void(TreeNode*, int)> dfs = [&](TreeNode* root, int mx) {
142+
if (!root) {
143+
return;
144+
}
145+
if (mx <= root->val) {
146+
++ans;
147+
mx = root->val;
148+
}
149+
dfs(root->left, mx);
150+
dfs(root->right, mx);
151+
};
152+
dfs(root, -1e6);
153+
return ans;
154+
}
155+
};
156+
```
157+
158+
### Go
159+
160+
```go
161+
/**
162+
* Definition for a binary tree node.
163+
* type TreeNode struct {
164+
* Val int
165+
* Left *TreeNode
166+
* Right *TreeNode
167+
* }
168+
*/
169+
func goodNodes(root *TreeNode) (ans int) {
170+
var dfs func(*TreeNode, int)
171+
dfs = func(root *TreeNode, mx int) {
172+
if root == nil {
173+
return
174+
}
175+
if mx <= root.Val {
176+
ans++
177+
mx = root.Val
178+
}
179+
dfs(root.Left, mx)
180+
dfs(root.Right, mx)
181+
}
182+
dfs(root, -10001)
183+
return
184+
}
185+
```
186+
187+
### TypeScript
188+
189+
```ts
190+
/**
191+
* Definition for a binary tree node.
192+
* class TreeNode {
193+
* val: number
194+
* left: TreeNode | null
195+
* right: TreeNode | null
196+
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
197+
* this.val = (val===undefined ? 0 : val)
198+
* this.left = (left===undefined ? null : left)
199+
* this.right = (right===undefined ? null : right)
200+
* }
201+
* }
202+
*/
203+
204+
function goodNodes(root: TreeNode | null): number {
205+
let ans = 0;
206+
const dfs = (root: TreeNode | null, mx: number) => {
207+
if (!root) {
208+
return;
209+
}
210+
if (mx <= root.val) {
211+
++ans;
212+
mx = root.val;
213+
}
214+
dfs(root.left, mx);
215+
dfs(root.right, mx);
216+
};
217+
dfs(root, -1e6);
218+
return ans;
219+
}
220+
```
221+
```
222+
223+
---
224+
225+
Let me know if you want the Chinese version (`README.md`) or to continue with more problems in this format!

0 commit comments

Comments
 (0)