From 70e680c8ac806701ebe01b2007b909eacf589580 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Fri, 5 Apr 2024 09:07:39 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.1026 No.1026.Maximum Difference Between Node and Ancestor --- .../README.md | 40 +++++++++++++- .../README_EN.md | 54 ++++++++++++++++++- .../Solution.cs | 33 ++++++++++++ .../Solution.py | 2 +- 4 files changed, 124 insertions(+), 5 deletions(-) create mode 100644 solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.cs diff --git a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README.md b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README.md index 9a68903565c6d..e59f2ba9f5677 100644 --- a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README.md +++ b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README.md @@ -50,7 +50,7 @@ ### 方法一:DFS -对于每个节点,求其与祖先节点的最大差值,我们只需要求出该节点与祖先节点最大值和最小值的差值,取所有差值的最大值即可。 +对于每个节点,求其与祖先节点的最大差值,我们只需要求出该节点与祖先节点最大值和最小值的差值。取所有节点与祖先节点差值的最大值即可。 因此,我们设计一个函数 $dfs(root, mi, mx)$,表示当前搜索到的节点为 $root$,其祖先节点的最大值为 $mx$,最小值为 $mi$,函数内更新最大差值 $ans$。 @@ -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 @@ -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); + } +} +``` + diff --git a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README_EN.md b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README_EN.md index c7aad24374261..59711f826ca6e 100644 --- a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README_EN.md +++ b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/README_EN.md @@ -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. @@ -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 @@ -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); + } +} +``` + diff --git a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.cs b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.cs new file mode 100644 index 0000000000000..fbfb97dfd4876 --- /dev/null +++ b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.cs @@ -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); + } +} \ No newline at end of file diff --git a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.py b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.py index 329d4d3c963dc..ca3f7e01a64fe 100644 --- a/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.py +++ b/solution/1000-1099/1026.Maximum Difference Between Node and Ancestor/Solution.py @@ -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