Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.1026 (#2538)
Browse files Browse the repository at this point in the history
No.1026.Maximum Difference Between Node and Ancestor
  • Loading branch information
yanglbme authored Apr 5, 2024
1 parent 5b47413 commit ee0739b
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@

### 方法一:DFS

对于每个节点,求其与祖先节点的最大差值,我们只需要求出该节点与祖先节点最大值和最小值的差值,取所有差值的最大值即可
对于每个节点,求其与祖先节点的最大差值,我们只需要求出该节点与祖先节点最大值和最小值的差值。取所有节点与祖先节点差值的最大值即可

因此,我们设计一个函数 $dfs(root, mi, mx)$,表示当前搜索到的节点为 $root$,其祖先节点的最大值为 $mx$,最小值为 $mi$,函数内更新最大差值 $ans$。

Expand All @@ -75,7 +75,7 @@
# self.right = right
class Solution:
def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
def dfs(root, mi, mx):
def dfs(root: Optional[TreeNode], mi: int, mx: int):
if root is None:
return
nonlocal ans
Expand Down Expand Up @@ -255,6 +255,42 @@ var maxAncestorDiff = function (root) {
};
```

```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private int ans;

public int MaxAncestorDiff(TreeNode root) {
dfs(root, root.val, root.val);
return ans;
}

private void dfs(TreeNode root, int mi, int mx) {
if (root == null) {
return;
}
int x = Math.Max(Math.Abs(mi - root.val), Math.Abs(mx - root.val));
ans = Math.Max(ans, x);
mi = Math.Min(mi, root.val);
mx = Math.Max(mx, root.val);
dfs(root.left, mi, mx);
dfs(root.right, mi, mx);
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,21 @@ Among all possible differences, the maximum value of 7 is obtained by |8 - 1| =

## Solutions

### Solution 1
### Solution 1: DFS

For each node, to find the maximum difference with its ancestor nodes, we only need to find the difference between the maximum and minimum values of the ancestor nodes. The maximum difference among all nodes and their ancestor nodes is the answer.

Therefore, we design a function $dfs(root, mi, mx)$, where the current node being searched is $root$, the maximum value of its ancestor nodes is $mx$, and the minimum value is $mi$. The function updates the maximum difference $ans$.

The logic of the function $dfs(root, mi, mx)$ is as follows:

- If $root$ is null, return directly.
- Otherwise, we update $ans = max(ans, |mi - root.val|, |mx - root.val|)$.
- Then update $mi = min(mi, root.val)$, $mx = max(mx, root.val)$, and recursively search the left and right subtrees.

In the main function, we call $dfs(root, root.val, root.val)$, and finally return $ans$.

The time complexity is $O(n)$, and the space complexity is $O(n)$. Where $n$ is the number of nodes in the binary tree.

<!-- tabs:start -->

Expand All @@ -53,7 +67,7 @@ Among all possible differences, the maximum value of 7 is obtained by |8 - 1| =
# self.right = right
class Solution:
def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
def dfs(root, mi, mx):
def dfs(root: Optional[TreeNode], mi: int, mx: int):
if root is None:
return
nonlocal ans
Expand Down Expand Up @@ -233,6 +247,42 @@ var maxAncestorDiff = function (root) {
};
```

```cs
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private int ans;

public int MaxAncestorDiff(TreeNode root) {
dfs(root, root.val, root.val);
return ans;
}

private void dfs(TreeNode root, int mi, int mx) {
if (root == null) {
return;
}
int x = Math.Max(Math.Abs(mi - root.val), Math.Abs(mx - root.val));
ans = Math.Max(ans, x);
mi = Math.Min(mi, root.val);
mx = Math.Max(mx, root.val);
dfs(root.left, mi, mx);
dfs(root.right, mi, mx);
}
}
```

<!-- tabs:end -->

<!-- end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* public int val;
* public TreeNode left;
* public TreeNode right;
* public TreeNode(int val=0, TreeNode left=null, TreeNode right=null) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
public class Solution {
private int ans;

public int MaxAncestorDiff(TreeNode root) {
dfs(root, root.val, root.val);
return ans;
}

private void dfs(TreeNode root, int mi, int mx) {
if (root == null) {
return;
}
int x = Math.Max(Math.Abs(mi - root.val), Math.Abs(mx - root.val));
ans = Math.Max(ans, x);
mi = Math.Min(mi, root.val);
mx = Math.Max(mx, root.val);
dfs(root.left, mi, mx);
dfs(root.right, mi, mx);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# self.right = right
class Solution:
def maxAncestorDiff(self, root: Optional[TreeNode]) -> int:
def dfs(root, mi, mx):
def dfs(root: Optional[TreeNode], mi: int, mx: int):
if root is None:
return
nonlocal ans
Expand Down

0 comments on commit ee0739b

Please sign in to comment.