diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md index 2e62530b63567..983c117496caa 100644 --- a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README.md @@ -59,7 +59,11 @@ tags: -### 方法一 +### 方法一:递归 + +我们首先判断 $\textit{root}$ 是否为空,若为空则返回 0。否则我们初始化一个变量 $\textit{mx}$ 用来记录子节点的最大深度,然后遍历 $\textit{root}$ 的所有子节点,递归调用 $\text{maxDepth}$ 函数,更新 $\textit{mx}$ 的值。最后返回 $\textit{mx} + 1$ 即可。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点的数量。 @@ -69,17 +73,20 @@ tags: """ # Definition for a Node. class Node: - def __init__(self, val=None, children=None): + def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None): self.val = val self.children = children """ class Solution: - def maxDepth(self, root: 'Node') -> int: + def maxDepth(self, root: "Node") -> int: if root is None: return 0 - return 1 + max([self.maxDepth(child) for child in root.children], default=0) + mx = 0 + for child in root.children: + mx = max(mx, self.maxDepth(child)) + return 1 + mx ``` #### Java @@ -109,11 +116,11 @@ class Solution { if (root == null) { return 0; } - int ans = 1; + int mx = 0; for (Node child : root.children) { - ans = Math.max(ans, 1 + maxDepth(child)); + mx = Math.max(mx, maxDepth(child)); } - return ans; + return 1 + mx; } } ``` @@ -144,10 +151,14 @@ public: class Solution { public: int maxDepth(Node* root) { - if (!root) return 0; - int ans = 1; - for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child)); - return ans; + if (!root) { + return 0; + } + int mx = 0; + for (Node* child : root->children) { + mx = max(mx, maxDepth(child)); + } + return mx + 1; } }; ``` @@ -167,11 +178,35 @@ func maxDepth(root *Node) int { if root == nil { return 0 } - ans := 1 + mx := 0 for _, child := range root.Children { - ans = max(ans, 1+maxDepth(child)) + mx = max(mx, maxDepth(child)) } - return ans + return 1 + mx +} +``` + +#### TypeScript + +```ts +/** + * Definition for _Node. + * class _Node { + * val: number + * children: _Node[] + * + * constructor(val?: number, children?: _Node[]) { + * this.val = (val===undefined ? 0 : val) + * this.children = (children===undefined ? [] : children) + * } + * } + */ + +function maxDepth(root: _Node | null): number { + if (!root) { + return 0; + } + return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0); } ``` diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md index e22ba845c876f..00f282179e710 100644 --- a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/README_EN.md @@ -57,7 +57,11 @@ tags: -### Solution 1 +### Solution 1: Recursion + +First, we check if $\textit{root}$ is null. If it is, we return 0. Otherwise, we initialize a variable $\textit{mx}$ to record the maximum depth of the child nodes, then traverse all the child nodes of $\textit{root}$, recursively call the $\text{maxDepth}$ function, and update the value of $\textit{mx}$. Finally, we return $\textit{mx} + 1$. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes. @@ -67,17 +71,20 @@ tags: """ # Definition for a Node. class Node: - def __init__(self, val=None, children=None): + def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None): self.val = val self.children = children """ class Solution: - def maxDepth(self, root: 'Node') -> int: + def maxDepth(self, root: "Node") -> int: if root is None: return 0 - return 1 + max([self.maxDepth(child) for child in root.children], default=0) + mx = 0 + for child in root.children: + mx = max(mx, self.maxDepth(child)) + return 1 + mx ``` #### Java @@ -107,11 +114,11 @@ class Solution { if (root == null) { return 0; } - int ans = 1; + int mx = 0; for (Node child : root.children) { - ans = Math.max(ans, 1 + maxDepth(child)); + mx = Math.max(mx, maxDepth(child)); } - return ans; + return 1 + mx; } } ``` @@ -142,10 +149,14 @@ public: class Solution { public: int maxDepth(Node* root) { - if (!root) return 0; - int ans = 1; - for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child)); - return ans; + if (!root) { + return 0; + } + int mx = 0; + for (Node* child : root->children) { + mx = max(mx, maxDepth(child)); + } + return mx + 1; } }; ``` @@ -165,11 +176,35 @@ func maxDepth(root *Node) int { if root == nil { return 0 } - ans := 1 + mx := 0 for _, child := range root.Children { - ans = max(ans, 1+maxDepth(child)) + mx = max(mx, maxDepth(child)) } - return ans + return 1 + mx +} +``` + +#### TypeScript + +```ts +/** + * Definition for _Node. + * class _Node { + * val: number + * children: _Node[] + * + * constructor(val?: number, children?: _Node[]) { + * this.val = (val===undefined ? 0 : val) + * this.children = (children===undefined ? [] : children) + * } + * } + */ + +function maxDepth(root: _Node | null): number { + if (!root) { + return 0; + } + return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0); } ``` diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.cpp b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.cpp index c2d959a1940d1..75e889f65d292 100644 --- a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.cpp +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.cpp @@ -21,9 +21,13 @@ class Node { class Solution { public: int maxDepth(Node* root) { - if (!root) return 0; - int ans = 1; - for (auto& child : root->children) ans = max(ans, 1 + maxDepth(child)); - return ans; + if (!root) { + return 0; + } + int mx = 0; + for (Node* child : root->children) { + mx = max(mx, maxDepth(child)); + } + return mx + 1; } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.go b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.go index 445451d86f048..8aaf065bb80da 100644 --- a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.go +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.go @@ -10,9 +10,9 @@ func maxDepth(root *Node) int { if root == nil { return 0 } - ans := 1 + mx := 0 for _, child := range root.Children { - ans = max(ans, 1+maxDepth(child)) + mx = max(mx, maxDepth(child)) } - return ans -} \ No newline at end of file + return 1 + mx +} diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.java b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.java index b3fbf183f5424..111d0b781b188 100644 --- a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.java +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.java @@ -22,10 +22,10 @@ public int maxDepth(Node root) { if (root == null) { return 0; } - int ans = 1; + int mx = 0; for (Node child : root.children) { - ans = Math.max(ans, 1 + maxDepth(child)); + mx = Math.max(mx, maxDepth(child)); } - return ans; + return 1 + mx; } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.py b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.py index a29f3ed859130..98c1895eaade8 100644 --- a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.py +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.py @@ -1,14 +1,17 @@ """ # Definition for a Node. class Node: - def __init__(self, val=None, children=None): + def __init__(self, val: Optional[int] = None, children: Optional[List['Node']] = None): self.val = val self.children = children """ class Solution: - def maxDepth(self, root: 'Node') -> int: + def maxDepth(self, root: "Node") -> int: if root is None: return 0 - return 1 + max([self.maxDepth(child) for child in root.children], default=0) + mx = 0 + for child in root.children: + mx = max(mx, self.maxDepth(child)) + return 1 + mx diff --git a/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.ts b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.ts new file mode 100644 index 0000000000000..d68a4b482906e --- /dev/null +++ b/solution/0500-0599/0559.Maximum Depth of N-ary Tree/Solution.ts @@ -0,0 +1,19 @@ +/** + * Definition for _Node. + * class _Node { + * val: number + * children: _Node[] + * + * constructor(val?: number, children?: _Node[]) { + * this.val = (val===undefined ? 0 : val) + * this.children = (children===undefined ? [] : children) + * } + * } + */ + +function maxDepth(root: _Node | null): number { + if (!root) { + return 0; + } + return 1 + Math.max(...root.children.map(child => maxDepth(child)), 0); +} diff --git a/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README.md b/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README.md index 361ad5a3c3226..598d48f20fd11 100644 --- a/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README.md +++ b/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README.md @@ -68,7 +68,7 @@ tags: 遍历矩阵,当遇到 $1$ 时,更新 $f[i][j][k]$ 的值。对于每个位置 $(i, j)$,我们只需要更新其四个方向的值即可。然后更新答案。 -时间复杂度 $O(m\times n)$,空间复杂度 $O(m\times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 +时间复杂度 $O(m \times n)$,空间复杂度 $O(m \times n)$。其中 $m$ 和 $n$ 分别为矩阵的行数和列数。 diff --git a/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README_EN.md b/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README_EN.md index 1de17cddf10cb..3b04ffa448fa9 100644 --- a/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README_EN.md +++ b/solution/0500-0599/0562.Longest Line of Consecutive One in Matrix/README_EN.md @@ -54,7 +54,15 @@ tags: -### Solution 1 +### Solution 1: Dynamic Programming + +We define $f[i][j][k]$ to represent the length of the longest consecutive $1$s ending at $(i, j)$ in direction $k$. The value range of $k$ is $0, 1, 2, 3$, representing horizontal, vertical, diagonal, and anti-diagonal directions, respectively. + +> We can also use four 2D arrays to represent the length of the longest consecutive $1$s in the four directions. + +We traverse the matrix, and when we encounter $1$, we update the value of $f[i][j][k]$. For each position $(i, j)$, we only need to update the values in its four directions. Then we update the answer. + +The time complexity is $O(m \times n)$, and the space complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns in the matrix, respectively. diff --git a/solution/0500-0599/0563.Binary Tree Tilt/README.md b/solution/0500-0599/0563.Binary Tree Tilt/README.md index dc1ff5a05ab75..025499f3fa99d 100644 --- a/solution/0500-0599/0563.Binary Tree Tilt/README.md +++ b/solution/0500-0599/0563.Binary Tree Tilt/README.md @@ -75,7 +75,13 @@ tags: -### 方法一 +### 方法一:递归 + +我们设计一个函数 $\text{dfs}$,用来计算以当前节点为根节点的子树的节点之和。在 $\text{dfs}$ 函数中,我们首先判断当前节点是否为空,若为空则返回 0。然后递归调用 $\text{dfs}$ 函数计算左子树的节点之和 $l$ 和右子树的节点之和 $r$。接着计算当前节点的坡度,即 $|l - r|$,并将其加到答案中。最后返回当前节点的节点之和 $l + r + \textit{root.val}$。 + +在主函数中,我们初始化答案为 0,然后调用 $\text{dfs}$ 函数计算整个树的坡度,并返回答案。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 为节点的数量。 @@ -89,19 +95,17 @@ tags: # self.left = left # self.right = right class Solution: - def findTilt(self, root: TreeNode) -> int: - ans = 0 - - def sum(root): + def findTilt(self, root: Optional[TreeNode]) -> int: + def dfs(root: Optional[TreeNode]) -> int: if root is None: return 0 + l, r = dfs(root.left), dfs(root.right) nonlocal ans - left = sum(root.left) - right = sum(root.right) - ans += abs(left - right) - return root.val + left + right + ans += abs(l - r) + return l + r + root.val - sum(root) + ans = 0 + dfs(root) return ans ``` @@ -127,19 +131,17 @@ class Solution { private int ans; public int findTilt(TreeNode root) { - ans = 0; - sum(root); + dfs(root); return ans; } - private int sum(TreeNode root) { + private int dfs(TreeNode root) { if (root == null) { return 0; } - int left = sum(root.left); - int right = sum(root.right); - ans += Math.abs(left - right); - return root.val + left + right; + int l = dfs(root.left), r = dfs(root.right); + ans += Math.abs(l - r); + return l + r + root.val; } } ``` @@ -160,20 +162,19 @@ class Solution { */ class Solution { public: - int ans; - int findTilt(TreeNode* root) { - ans = 0; - sum(root); + int ans = 0; + auto dfs = [&](this auto&& dfs, TreeNode* root) -> int { + if (!root) { + return 0; + } + int l = dfs(root->left), r = dfs(root->right); + ans += abs(l - r); + return l + r + root->val; + }; + dfs(root); return ans; } - - int sum(TreeNode* root) { - if (!root) return 0; - int left = sum(root->left), right = sum(root->right); - ans += abs(left - right); - return root->val + left + right; - } }; ``` @@ -188,28 +189,57 @@ public: * Right *TreeNode * } */ -var ans int - -func findTilt(root *TreeNode) int { - ans = 0 - sum(root) - return ans -} - -func sum(root *TreeNode) int { - if root == nil { - return 0 +func findTilt(root *TreeNode) (ans int) { + var dfs func(*TreeNode) int + dfs = func(root *TreeNode) int { + if root == nil { + return 0 + } + l, r := dfs(root.Left), dfs(root.Right) + ans += abs(l - r) + return l + r + root.Val } - left, right := sum(root.Left), sum(root.Right) - ans += abs(left - right) - return root.Val + left + right + dfs(root) + return } func abs(x int) int { - if x > 0 { - return x + if x < 0 { + return -x } - return -x + return x +} +``` + +#### TypeScript + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function findTilt(root: TreeNode | null): number { + let ans: number = 0; + const dfs = (root: TreeNode | null): number => { + if (!root) { + return 0; + } + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans += Math.abs(l - r); + return l + r + root.val; + }; + dfs(root); + return ans; } ``` diff --git a/solution/0500-0599/0563.Binary Tree Tilt/README_EN.md b/solution/0500-0599/0563.Binary Tree Tilt/README_EN.md index ac323a07bd1f4..818f0d44432be 100644 --- a/solution/0500-0599/0563.Binary Tree Tilt/README_EN.md +++ b/solution/0500-0599/0563.Binary Tree Tilt/README_EN.md @@ -28,7 +28,7 @@ tags:
Input: root = [1,2,3] Output: 1 -Explanation: +Explanation: Tilt of node 2 : |0-0| = 0 (no children) Tilt of node 3 : |0-0| = 0 (no children) Tilt of node 1 : |2-3| = 1 (left subtree is just left child, so sum is 2; right subtree is just right child, so sum is 3) @@ -40,7 +40,7 @@ Sum of every tilt : 0 + 0 + 1 = 1Input: root = [4,2,9,3,5,null,7] Output: 15 -Explanation: +Explanation: Tilt of node 3 : |0-0| = 0 (no children) Tilt of node 5 : |0-0| = 0 (no children) Tilt of node 7 : |0-0| = 0 (no children) @@ -71,7 +71,13 @@ Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15 -### Solution 1 +### Solution 1: Recursion + +We design a function $\text{dfs}$ to calculate the sum of nodes in the subtree rooted at the current node. In the $\text{dfs}$ function, we first check if the current node is null. If it is, we return 0. Then we recursively call the $\text{dfs}$ function to calculate the sum of nodes in the left subtree $l$ and the sum of nodes in the right subtree $r$. Next, we calculate the tilt of the current node, which is $|l - r|$, and add it to the answer. Finally, we return the sum of nodes of the current node, which is $l + r + \textit{root.val}$. + +In the main function, we initialize the answer to 0, then call the $\text{dfs}$ function to calculate the tilt of the entire tree and return the answer. + +The time complexity is $O(n)$, and the space complexity is $O(n)$, where $n$ is the number of nodes. @@ -85,19 +91,17 @@ Sum of every tilt : 0 + 0 + 0 + 2 + 7 + 6 = 15 # self.left = left # self.right = right class Solution: - def findTilt(self, root: TreeNode) -> int: - ans = 0 - - def sum(root): + def findTilt(self, root: Optional[TreeNode]) -> int: + def dfs(root: Optional[TreeNode]) -> int: if root is None: return 0 + l, r = dfs(root.left), dfs(root.right) nonlocal ans - left = sum(root.left) - right = sum(root.right) - ans += abs(left - right) - return root.val + left + right + ans += abs(l - r) + return l + r + root.val - sum(root) + ans = 0 + dfs(root) return ans ``` @@ -123,19 +127,17 @@ class Solution { private int ans; public int findTilt(TreeNode root) { - ans = 0; - sum(root); + dfs(root); return ans; } - private int sum(TreeNode root) { + private int dfs(TreeNode root) { if (root == null) { return 0; } - int left = sum(root.left); - int right = sum(root.right); - ans += Math.abs(left - right); - return root.val + left + right; + int l = dfs(root.left), r = dfs(root.right); + ans += Math.abs(l - r); + return l + r + root.val; } } ``` @@ -156,20 +158,19 @@ class Solution { */ class Solution { public: - int ans; - int findTilt(TreeNode* root) { - ans = 0; - sum(root); + int ans = 0; + auto dfs = [&](this auto&& dfs, TreeNode* root) -> int { + if (!root) { + return 0; + } + int l = dfs(root->left), r = dfs(root->right); + ans += abs(l - r); + return l + r + root->val; + }; + dfs(root); return ans; } - - int sum(TreeNode* root) { - if (!root) return 0; - int left = sum(root->left), right = sum(root->right); - ans += abs(left - right); - return root->val + left + right; - } }; ``` @@ -184,28 +185,57 @@ public: * Right *TreeNode * } */ -var ans int - -func findTilt(root *TreeNode) int { - ans = 0 - sum(root) - return ans -} - -func sum(root *TreeNode) int { - if root == nil { - return 0 +func findTilt(root *TreeNode) (ans int) { + var dfs func(*TreeNode) int + dfs = func(root *TreeNode) int { + if root == nil { + return 0 + } + l, r := dfs(root.Left), dfs(root.Right) + ans += abs(l - r) + return l + r + root.Val } - left, right := sum(root.Left), sum(root.Right) - ans += abs(left - right) - return root.Val + left + right + dfs(root) + return } func abs(x int) int { - if x > 0 { - return x + if x < 0 { + return -x } - return -x + return x +} +``` + +#### TypeScript + +```ts +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function findTilt(root: TreeNode | null): number { + let ans: number = 0; + const dfs = (root: TreeNode | null): number => { + if (!root) { + return 0; + } + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans += Math.abs(l - r); + return l + r + root.val; + }; + dfs(root); + return ans; } ``` diff --git a/solution/0500-0599/0563.Binary Tree Tilt/Solution.cpp b/solution/0500-0599/0563.Binary Tree Tilt/Solution.cpp index 1ca20e7cab9ee..6dccdaa121de6 100644 --- a/solution/0500-0599/0563.Binary Tree Tilt/Solution.cpp +++ b/solution/0500-0599/0563.Binary Tree Tilt/Solution.cpp @@ -11,18 +11,17 @@ */ class Solution { public: - int ans; - int findTilt(TreeNode* root) { - ans = 0; - sum(root); + int ans = 0; + auto dfs = [&](this auto&& dfs, TreeNode* root) -> int { + if (!root) { + return 0; + } + int l = dfs(root->left), r = dfs(root->right); + ans += abs(l - r); + return l + r + root->val; + }; + dfs(root); return ans; } - - int sum(TreeNode* root) { - if (!root) return 0; - int left = sum(root->left), right = sum(root->right); - ans += abs(left - right); - return root->val + left + right; - } -}; \ No newline at end of file +}; diff --git a/solution/0500-0599/0563.Binary Tree Tilt/Solution.go b/solution/0500-0599/0563.Binary Tree Tilt/Solution.go index 1fc9c98235902..c3956d8d6a7ae 100644 --- a/solution/0500-0599/0563.Binary Tree Tilt/Solution.go +++ b/solution/0500-0599/0563.Binary Tree Tilt/Solution.go @@ -6,26 +6,23 @@ * Right *TreeNode * } */ -var ans int - -func findTilt(root *TreeNode) int { - ans = 0 - sum(root) - return ans -} - -func sum(root *TreeNode) int { - if root == nil { - return 0 +func findTilt(root *TreeNode) (ans int) { + var dfs func(*TreeNode) int + dfs = func(root *TreeNode) int { + if root == nil { + return 0 + } + l, r := dfs(root.Left), dfs(root.Right) + ans += abs(l - r) + return l + r + root.Val } - left, right := sum(root.Left), sum(root.Right) - ans += abs(left - right) - return root.Val + left + right + dfs(root) + return } func abs(x int) int { - if x > 0 { - return x + if x < 0 { + return -x } - return -x -} \ No newline at end of file + return x +} diff --git a/solution/0500-0599/0563.Binary Tree Tilt/Solution.java b/solution/0500-0599/0563.Binary Tree Tilt/Solution.java index f03febf02b52b..1db5d9c38bdfc 100644 --- a/solution/0500-0599/0563.Binary Tree Tilt/Solution.java +++ b/solution/0500-0599/0563.Binary Tree Tilt/Solution.java @@ -17,18 +17,16 @@ class Solution { private int ans; public int findTilt(TreeNode root) { - ans = 0; - sum(root); + dfs(root); return ans; } - private int sum(TreeNode root) { + private int dfs(TreeNode root) { if (root == null) { return 0; } - int left = sum(root.left); - int right = sum(root.right); - ans += Math.abs(left - right); - return root.val + left + right; + int l = dfs(root.left), r = dfs(root.right); + ans += Math.abs(l - r); + return l + r + root.val; } -} \ No newline at end of file +} diff --git a/solution/0500-0599/0563.Binary Tree Tilt/Solution.py b/solution/0500-0599/0563.Binary Tree Tilt/Solution.py index 96fb38e618e63..0f799a54101c5 100644 --- a/solution/0500-0599/0563.Binary Tree Tilt/Solution.py +++ b/solution/0500-0599/0563.Binary Tree Tilt/Solution.py @@ -5,17 +5,15 @@ # self.left = left # self.right = right class Solution: - def findTilt(self, root: TreeNode) -> int: - ans = 0 - - def sum(root): + def findTilt(self, root: Optional[TreeNode]) -> int: + def dfs(root: Optional[TreeNode]) -> int: if root is None: return 0 + l, r = dfs(root.left), dfs(root.right) nonlocal ans - left = sum(root.left) - right = sum(root.right) - ans += abs(left - right) - return root.val + left + right + ans += abs(l - r) + return l + r + root.val - sum(root) + ans = 0 + dfs(root) return ans diff --git a/solution/0500-0599/0563.Binary Tree Tilt/Solution.ts b/solution/0500-0599/0563.Binary Tree Tilt/Solution.ts new file mode 100644 index 0000000000000..cc5f70a65886b --- /dev/null +++ b/solution/0500-0599/0563.Binary Tree Tilt/Solution.ts @@ -0,0 +1,27 @@ +/** + * Definition for a binary tree node. + * class TreeNode { + * val: number + * left: TreeNode | null + * right: TreeNode | null + * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { + * this.val = (val===undefined ? 0 : val) + * this.left = (left===undefined ? null : left) + * this.right = (right===undefined ? null : right) + * } + * } + */ + +function findTilt(root: TreeNode | null): number { + let ans: number = 0; + const dfs = (root: TreeNode | null): number => { + if (!root) { + return 0; + } + const [l, r] = [dfs(root.left), dfs(root.right)]; + ans += Math.abs(l - r); + return l + r + root.val; + }; + dfs(root); + return ans; +} diff --git a/solution/0500-0599/0566.Reshape the Matrix/README_EN.md b/solution/0500-0599/0566.Reshape the Matrix/README_EN.md index fa4bc220d8cd8..a8b4f4f528e77 100644 --- a/solution/0500-0599/0566.Reshape the Matrix/README_EN.md +++ b/solution/0500-0599/0566.Reshape the Matrix/README_EN.md @@ -58,7 +58,15 @@ tags: -### Solution 1 +### Solution 1: Simulation + +First, we get the number of rows and columns of the original matrix, denoted as $m$ and $n$ respectively. If $m \times n \neq r \times c$, then the matrix cannot be reshaped, and we return the original matrix directly. + +Otherwise, we create a new matrix with $r$ rows and $c$ columns. Starting from the first element of the original matrix, we traverse all elements in row-major order and place the traversed elements into the new matrix in order. + +After traversing all elements of the original matrix, we get the answer. + +The time complexity is $O(m \times n)$, where $m$ and $n$ are the number of rows and columns of the original matrix, respectively. Ignoring the space consumption of the answer, the space complexity is $O(1)$.