Skip to content

Commit 828539e

Browse files
committed
feat: add solutions to lc problems: No.1026,1893
* No.1026.Maximum Difference Between Node and Ancestor * No.1893.Check if All the Integers in a Range Are Covered
1 parent 74682e2 commit 828539e

File tree

10 files changed

+474
-57
lines changed

10 files changed

+474
-57
lines changed

solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README.md

+145-1
Original file line numberDiff line numberDiff line change
@@ -48,22 +48,166 @@
4848

4949
<!-- 这里可写通用的实现逻辑 -->
5050

51+
将节点的最大值、最小值自上而下传递,此过程中迭代求最大差值。
52+
5153
<!-- tabs:start -->
5254

5355
### **Python3**
5456

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

5759
```python
58-
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 maxAncestorDiff(self, root: TreeNode) -> int:
68+
def dfs(root, mx, mi):
69+
if root is None:
70+
return
71+
nonlocal ans
72+
ans = max(ans, abs(root.val - mx), abs(root.val - mi))
73+
mx = max(mx, root.val)
74+
mi = min(mi, root.val)
75+
dfs(root.left, mx, mi)
76+
dfs(root.right, mx, mi)
77+
78+
ans = 0
79+
dfs(root, root.val, root.val)
80+
return ans
5981
```
6082

6183
### **Java**
6284

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

6587
```java
88+
/**
89+
* Definition for a binary tree node.
90+
* public class TreeNode {
91+
* int val;
92+
* TreeNode left;
93+
* TreeNode right;
94+
* TreeNode() {}
95+
* TreeNode(int val) { this.val = val; }
96+
* TreeNode(int val, TreeNode left, TreeNode right) {
97+
* this.val = val;
98+
* this.left = left;
99+
* this.right = right;
100+
* }
101+
* }
102+
*/
103+
class Solution {
104+
private int ans;
105+
106+
public int maxAncestorDiff(TreeNode root) {
107+
ans = 0;
108+
dfs(root, root.val, root.val);
109+
return ans;
110+
}
111+
112+
private void dfs(TreeNode root, int mx, int mi) {
113+
if (root == null) {
114+
return;
115+
}
116+
int t = Math.max(Math.abs(root.val - mx), Math.abs(root.val - mi));
117+
ans = Math.max(ans, t);
118+
mx = Math.max(mx, root.val);
119+
mi = Math.min(mi, root.val);
120+
dfs(root.left, mx, mi);
121+
dfs(root.right, mx, mi);
122+
}
123+
}
124+
```
125+
126+
### **C++**
127+
128+
```cpp
129+
/**
130+
* Definition for a binary tree node.
131+
* struct TreeNode {
132+
* int val;
133+
* TreeNode *left;
134+
* TreeNode *right;
135+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
136+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
137+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
138+
* };
139+
*/
140+
class Solution {
141+
public:
142+
int ans;
143+
144+
int maxAncestorDiff(TreeNode* root) {
145+
ans = 0;
146+
dfs(root, root->val, root->val);
147+
return ans;
148+
}
149+
150+
void dfs(TreeNode* root, int mx, int mi) {
151+
if (!root) return;
152+
int t = max(abs(root->val - mx), abs(root->val - mi));
153+
ans = max(ans, t);
154+
mx = max(mx, root->val);
155+
mi = min(mi, root->val);
156+
dfs(root->left, mx, mi);
157+
dfs(root->right, mx, mi);
158+
}
159+
};
160+
```
66161
162+
### **Go**
163+
164+
```go
165+
/**
166+
* Definition for a binary tree node.
167+
* type TreeNode struct {
168+
* Val int
169+
* Left *TreeNode
170+
* Right *TreeNode
171+
* }
172+
*/
173+
func maxAncestorDiff(root *TreeNode) int {
174+
ans := 0
175+
var dfs func(root *TreeNode, mx, mi int)
176+
dfs = func(root *TreeNode, mx, mi int) {
177+
if root == nil {
178+
return
179+
}
180+
t := max(abs(root.Val-mx), abs(root.Val-mi))
181+
ans = max(ans, t)
182+
mx = max(mx, root.Val)
183+
mi = min(mi, root.Val)
184+
dfs(root.Left, mx, mi)
185+
dfs(root.Right, mx, mi)
186+
}
187+
dfs(root, root.Val, root.Val)
188+
return ans
189+
}
190+
191+
func abs(x int) int {
192+
if x > 0 {
193+
return x
194+
}
195+
return -x
196+
}
197+
198+
func max(a, b int) int {
199+
if a > b {
200+
return a
201+
}
202+
return b
203+
}
204+
205+
func min(a, b int) int {
206+
if a < b {
207+
return a
208+
}
209+
return b
210+
}
67211
```
68212

69213
### **...**

solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README_EN.md

+143-1
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,155 @@ Among all possible differences, the maximum value of 7 is obtained by |8 - 1| =
4343
### **Python3**
4444

4545
```python
46-
46+
# Definition for a binary tree node.
47+
# class TreeNode:
48+
# def __init__(self, val=0, left=None, right=None):
49+
# self.val = val
50+
# self.left = left
51+
# self.right = right
52+
class Solution:
53+
def maxAncestorDiff(self, root: TreeNode) -> int:
54+
def dfs(root, mx, mi):
55+
if root is None:
56+
return
57+
nonlocal ans
58+
ans = max(ans, abs(root.val - mx), abs(root.val - mi))
59+
mx = max(mx, root.val)
60+
mi = min(mi, root.val)
61+
dfs(root.left, mx, mi)
62+
dfs(root.right, mx, mi)
63+
64+
ans = 0
65+
dfs(root, root.val, root.val)
66+
return ans
4767
```
4868

4969
### **Java**
5070

5171
```java
72+
/**
73+
* Definition for a binary tree node.
74+
* public class TreeNode {
75+
* int val;
76+
* TreeNode left;
77+
* TreeNode right;
78+
* TreeNode() {}
79+
* TreeNode(int val) { this.val = val; }
80+
* TreeNode(int val, TreeNode left, TreeNode right) {
81+
* this.val = val;
82+
* this.left = left;
83+
* this.right = right;
84+
* }
85+
* }
86+
*/
87+
class Solution {
88+
private int ans;
89+
90+
public int maxAncestorDiff(TreeNode root) {
91+
ans = 0;
92+
dfs(root, root.val, root.val);
93+
return ans;
94+
}
95+
96+
private void dfs(TreeNode root, int mx, int mi) {
97+
if (root == null) {
98+
return;
99+
}
100+
int t = Math.max(Math.abs(root.val - mx), Math.abs(root.val - mi));
101+
ans = Math.max(ans, t);
102+
mx = Math.max(mx, root.val);
103+
mi = Math.min(mi, root.val);
104+
dfs(root.left, mx, mi);
105+
dfs(root.right, mx, mi);
106+
}
107+
}
108+
```
109+
110+
### **C++**
111+
112+
```cpp
113+
/**
114+
* Definition for a binary tree node.
115+
* struct TreeNode {
116+
* int val;
117+
* TreeNode *left;
118+
* TreeNode *right;
119+
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
120+
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
121+
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
122+
* };
123+
*/
124+
class Solution {
125+
public:
126+
int ans;
127+
128+
int maxAncestorDiff(TreeNode* root) {
129+
ans = 0;
130+
dfs(root, root->val, root->val);
131+
return ans;
132+
}
133+
134+
void dfs(TreeNode* root, int mx, int mi) {
135+
if (!root) return;
136+
int t = max(abs(root->val - mx), abs(root->val - mi));
137+
ans = max(ans, t);
138+
mx = max(mx, root->val);
139+
mi = min(mi, root->val);
140+
dfs(root->left, mx, mi);
141+
dfs(root->right, mx, mi);
142+
}
143+
};
144+
```
52145
146+
### **Go**
147+
148+
```go
149+
/**
150+
* Definition for a binary tree node.
151+
* type TreeNode struct {
152+
* Val int
153+
* Left *TreeNode
154+
* Right *TreeNode
155+
* }
156+
*/
157+
func maxAncestorDiff(root *TreeNode) int {
158+
ans := 0
159+
var dfs func(root *TreeNode, mx, mi int)
160+
dfs = func(root *TreeNode, mx, mi int) {
161+
if root == nil {
162+
return
163+
}
164+
t := max(abs(root.Val-mx), abs(root.Val-mi))
165+
ans = max(ans, t)
166+
mx = max(mx, root.Val)
167+
mi = min(mi, root.Val)
168+
dfs(root.Left, mx, mi)
169+
dfs(root.Right, mx, mi)
170+
}
171+
dfs(root, root.Val, root.Val)
172+
return ans
173+
}
174+
175+
func abs(x int) int {
176+
if x > 0 {
177+
return x
178+
}
179+
return -x
180+
}
181+
182+
func max(a, b int) int {
183+
if a > b {
184+
return a
185+
}
186+
return b
187+
}
188+
189+
func min(a, b int) int {
190+
if a < b {
191+
return a
192+
}
193+
return b
194+
}
53195
```
54196

55197
### **...**

solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.cpp

+18-28
Original file line numberDiff line numberDiff line change
@@ -4,38 +4,28 @@
44
* int val;
55
* TreeNode *left;
66
* TreeNode *right;
7-
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
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) {}
810
* };
911
*/
1012
class Solution {
11-
int helper(TreeNode* node, int& left, int & right) {
12-
left = INT_MAX;
13-
right = INT_MIN;
14-
if (node == nullptr) {
15-
return 0;
16-
}
17-
left = node->val;
18-
right = node->val;
19-
20-
int tmpLeft, tmpRight;
21-
int res = 0;
22-
23-
res = max(res, helper(node->left, tmpLeft, tmpRight));
24-
left = min(left, tmpLeft);
25-
right = max(right, tmpRight);
26-
27-
res = max(res, helper(node->right, tmpLeft, tmpRight));
28-
left = min(left, tmpLeft);
29-
right = max(right, tmpRight);
30-
31-
res = max(res, abs(node->val - left));
32-
res = max(res, abs(node->val - right));
33-
34-
return res;
35-
}
3613
public:
14+
int ans;
15+
3716
int maxAncestorDiff(TreeNode* root) {
38-
int left = 0, right = 0;
39-
return helper(root, left, right);
17+
ans = 0;
18+
dfs(root, root->val, root->val);
19+
return ans;
20+
}
21+
22+
void dfs(TreeNode* root, int mx, int mi) {
23+
if (!root) return;
24+
int t = max(abs(root->val - mx), abs(root->val - mi));
25+
ans = max(ans, t);
26+
mx = max(mx, root->val);
27+
mi = min(mi, root->val);
28+
dfs(root->left, mx, mi);
29+
dfs(root->right, mx, mi);
4030
}
4131
};

0 commit comments

Comments
 (0)