From 999a6437a4ff8bbc8dc24ace7ccd112cd6d3b48e Mon Sep 17 00:00:00 2001 From: yanglbme Date: Mon, 30 Aug 2021 21:00:55 +0800 Subject: [PATCH] feat: add solutions to lc/lcof2 problem:Sum Root to Leaf Numbers --- .../README.md" | 112 ++++++++++++++++-- .../Solution.cpp" | 26 ++++ .../Solution.go" | 22 ++++ .../Solution.java" | 31 +++++ .../Solution.py" | 17 +++ .../README.md" | 6 +- .../README.md" | 6 +- .../README.md" | 6 +- .../0129.Sum Root to Leaf Numbers/README.md | 108 ++++++++++++++++- .../README_EN.md | 108 ++++++++++++++++- .../Solution.cpp | 34 ++---- .../0129.Sum Root to Leaf Numbers/Solution.go | 30 +++-- .../Solution.java | 32 ++++- .../0129.Sum Root to Leaf Numbers/Solution.py | 17 +++ .../0200-0299/0261.Graph Valid Tree/README.md | 6 +- .../README.md | 6 +- .../0547.Number of Provinces/README.md | 6 +- .../0684.Redundant Connection/README.md | 6 +- .../0695.Max Area of Island/README.md | 6 +- .../0737.Sentence Similarity II/README.md | 6 +- .../0765.Couples Holding Hands/README.md | 6 +- .../0839.Similar String Groups/README.md | 6 +- .../0924.Minimize Malware Spread/README.md | 6 +- .../0928.Minimize Malware Spread II/README.md | 6 +- .../README.md | 6 +- .../README.md | 6 +- .../README.md | 6 +- 27 files changed, 537 insertions(+), 96 deletions(-) create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.go" create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.java" create mode 100644 "lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.py" create mode 100644 solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.py diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" index 680d2c2f45743..968cf24eaebfa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/README.md" @@ -63,6 +63,8 @@ +DFS。 + ### **Python3** @@ -70,7 +72,23 @@ ```python - +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + def dfs(root, presum): + if root is None: + return 0 + s = 10 * presum + root.val + if root.left is None and root.right is None: + return s + return dfs(root.left, s) + dfs(root.right, s) + + return dfs(root, 0) ``` ### **Java** @@ -78,13 +96,93 @@ ```java - -``` - -### **...** - +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public int sumNumbers(TreeNode root) { + return dfs(root, 0); + } + + private int dfs(TreeNode root, int presum) { + if (root == null) { + return 0; + } + int s = presum * 10 + root.val; + if (root.left == null && root.right == null) { + return s; + } + return dfs(root.left, s) + dfs(root.right, s); + } +} ``` +### **C++** + +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int sumNumbers(TreeNode *root) { + return dfs(root, 0); + } + + int dfs(TreeNode *root, int presum) { + if (root == nullptr) + return 0; + int s = presum * 10 + root->val; + if (root->left == nullptr && root->right == nullptr) + return s; + return dfs(root->left, s) + dfs(root->right, s); + } +}; ``` - +### **Go** + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumNumbers(root *TreeNode) int { + return dfs(root, 0) +} + +func dfs(root *TreeNode, presum int) int { + if root == nil { + return 0 + } + s := presum*10 + root.Val + if root.Left == nil && root.Right == nil { + return s + } + return dfs(root.Left, s) + dfs(root.Right, s) +} +``` \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" new file mode 100644 index 0000000000000..3bcf90a25c820 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.cpp" @@ -0,0 +1,26 @@ +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int sumNumbers(TreeNode *root) { + return dfs(root, 0); + } + + int dfs(TreeNode *root, int presum) { + if (root == nullptr) + return 0; + int s = presum * 10 + root->val; + if (root->left == nullptr && root->right == nullptr) + return s; + return dfs(root->left, s) + dfs(root->right, s); + } +}; \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.go" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.go" new file mode 100644 index 0000000000000..2c0f3fdf69c01 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.go" @@ -0,0 +1,22 @@ +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumNumbers(root *TreeNode) int { + return dfs(root, 0) +} + +func dfs(root *TreeNode, presum int) int { + if root == nil { + return 0 + } + s := presum*10 + root.Val + if root.Left == nil && root.Right == nil { + return s + } + return dfs(root.Left, s) + dfs(root.Right, s) +} \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.java" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.java" new file mode 100644 index 0000000000000..a42b54f2d91df --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.java" @@ -0,0 +1,31 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public int sumNumbers(TreeNode root) { + return dfs(root, 0); + } + + private int dfs(TreeNode root, int presum) { + if (root == null) { + return 0; + } + int s = presum * 10 + root.val; + if (root.left == null && root.right == null) { + return s; + } + return dfs(root.left, s) + dfs(root.right, s); + } +} \ No newline at end of file diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.py" "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.py" new file mode 100644 index 0000000000000..db8d64c45d6a9 --- /dev/null +++ "b/lcof2/\345\211\221\346\214\207 Offer II 049. \344\273\216\346\240\271\350\212\202\347\202\271\345\210\260\345\217\266\350\212\202\347\202\271\347\232\204\350\267\257\345\276\204\346\225\260\345\255\227\344\271\213\345\222\214/Solution.py" @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + def dfs(root, presum): + if root is None: + return 0 + s = 10 * presum + root.val + if root.left is None and root.right is None: + return s + return dfs(root.left, s) + dfs(root.right, s) + + return dfs(root, 0) diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" index b45eb3ede7824..77a94d86c37d0 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 105. \345\262\233\345\261\277\347\232\204\346\234\200\345\244\247\351\235\242\347\247\257/README.md" @@ -53,7 +53,7 @@ DFS 或并查集实现。 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -70,7 +70,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -90,7 +90,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" index 3bee32661dbd2..35c68c162ea6c 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 116. \346\234\213\345\217\213\345\234\210/README.md" @@ -60,7 +60,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -77,7 +77,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -97,7 +97,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git "a/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" "b/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" index 1653d837c21cb..023a44ff1bcaa 100644 --- "a/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" +++ "b/lcof2/\345\211\221\346\214\207 Offer II 118. \345\244\232\344\275\231\347\232\204\350\276\271/README.md" @@ -57,7 +57,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -74,7 +74,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -94,7 +94,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md index 6f6707e14c7bf..f5f5c12e86a1f 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README.md @@ -60,6 +60,8 @@ +DFS。 + ### **Python3** @@ -67,7 +69,23 @@ ```python - +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + def dfs(root, presum): + if root is None: + return 0 + s = 10 * presum + root.val + if root.left is None and root.right is None: + return s + return dfs(root.left, s) + dfs(root.right, s) + + return dfs(root, 0) ``` ### **Java** @@ -75,7 +93,95 @@ ```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public int sumNumbers(TreeNode root) { + return dfs(root, 0); + } + + private int dfs(TreeNode root, int presum) { + if (root == null) { + return 0; + } + int s = presum * 10 + root.val; + if (root.left == null && root.right == null) { + return s; + } + return dfs(root.left, s) + dfs(root.right, s); + } +} +``` + +### **C++** + +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int sumNumbers(TreeNode *root) { + return dfs(root, 0); + } + + int dfs(TreeNode *root, int presum) { + if (root == nullptr) + return 0; + int s = presum * 10 + root->val; + if (root->left == nullptr && root->right == nullptr) + return s; + return dfs(root->left, s) + dfs(root->right, s); + } +}; +``` +### **Go** + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumNumbers(root *TreeNode) int { + return dfs(root, 0) +} + +func dfs(root *TreeNode, presum int) int { + if root == nil { + return 0 + } + s := presum*10 + root.Val + if root.Left == nil && root.Right == nil { + return s + } + return dfs(root.Left, s) + dfs(root.Right, s) +} ``` ### **...** diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md index 695ce8fbd6d70..90296f1e54531 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/README_EN.md @@ -52,18 +52,124 @@ Therefore, sum = 495 + 491 + 40 = 1026. ## Solutions +DFS. + ### **Python3** ```python - +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + def dfs(root, presum): + if root is None: + return 0 + s = 10 * presum + root.val + if root.left is None and root.right is None: + return s + return dfs(root.left, s) + dfs(root.right, s) + + return dfs(root, 0) ``` ### **Java** ```java +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ +class Solution { + public int sumNumbers(TreeNode root) { + return dfs(root, 0); + } + + private int dfs(TreeNode root, int presum) { + if (root == null) { + return 0; + } + int s = presum * 10 + root.val; + if (root.left == null && root.right == null) { + return s; + } + return dfs(root.left, s) + dfs(root.right, s); + } +} +``` + +### **C++** + +```cpp +/** + * Definition for a binary tree node. + * struct TreeNode { + * int val; + * TreeNode *left; + * TreeNode *right; + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} + * }; + */ +class Solution { +public: + int sumNumbers(TreeNode *root) { + return dfs(root, 0); + } + + int dfs(TreeNode *root, int presum) { + if (root == nullptr) + return 0; + int s = presum * 10 + root->val; + if (root->left == nullptr && root->right == nullptr) + return s; + return dfs(root->left, s) + dfs(root->right, s); + } +}; +``` +### **Go** + +```go +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ +func sumNumbers(root *TreeNode) int { + return dfs(root, 0) +} + +func dfs(root *TreeNode, presum int) int { + if root == nil { + return 0 + } + s := presum*10 + root.Val + if root.Left == nil && root.Right == nil { + return s + } + return dfs(root.Left, s) + dfs(root.Right, s) +} ``` ### **...** diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp index d2a12714894c3..bf2d1e1b2d64c 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.cpp @@ -4,31 +4,23 @@ * int val; * TreeNode *left; * TreeNode *right; - * TreeNode(int x) : val(x), left(NULL), right(NULL) {} + * TreeNode() : val(0), left(nullptr), right(nullptr) {} + * TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} + * TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {} * }; */ class Solution { public: - int sumNumbers(TreeNode* root) { - if (!root) return 0; - int res = 0; - stack st{{root}}; - while (!st.empty()) { - TreeNode* t = st.top(); - st.pop(); - if (!t->left && !t->right) { - res += t->val; - } - if (t->right) { - t->right->val += t->val * 10; - st.push(t->right); - } - if (t->left) { - t->left->val += t->val * 10; - st.push(t->left); - } - } - return res; + int sumNumbers(TreeNode *root) { + return dfs(root, 0); } + int dfs(TreeNode *root, int presum) { + if (root == nullptr) + return 0; + int s = presum * 10 + root->val; + if (root->left == nullptr && root->right == nullptr) + return s; + return dfs(root->left, s) + dfs(root->right, s); + } }; \ No newline at end of file diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.go b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.go index 92b6a15aba85a..2c0f3fdf69c01 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.go +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.go @@ -1,16 +1,22 @@ +/** + * Definition for a binary tree node. + * type TreeNode struct { + * Val int + * Left *TreeNode + * Right *TreeNode + * } + */ func sumNumbers(root *TreeNode) int { - if root == nil { - return 0 - } - return dfs(root, 0) + return dfs(root, 0) } -func dfs(root *TreeNode, sum int) int { - if root == nil { - return 0 - } - if root.Left == nil && root.Right == nil { - return 10*sum + root.Val - } - return dfs(root.Left, 10*sum + root.Val) + dfs(root.Right, 10*sum + root.Val) +func dfs(root *TreeNode, presum int) int { + if root == nil { + return 0 + } + s := presum*10 + root.Val + if root.Left == nil && root.Right == nil { + return s + } + return dfs(root.Left, s) + dfs(root.Right, s) } \ No newline at end of file diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.java b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.java index 4434a2bfaac59..a42b54f2d91df 100644 --- a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.java +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.java @@ -1,11 +1,31 @@ +/** + * Definition for a binary tree node. + * public class TreeNode { + * int val; + * TreeNode left; + * TreeNode right; + * TreeNode() {} + * TreeNode(int val) { this.val = val; } + * TreeNode(int val, TreeNode left, TreeNode right) { + * this.val = val; + * this.left = left; + * this.right = right; + * } + * } + */ class Solution { public int sumNumbers(TreeNode root) { - return sumNumbers(root,0); + return dfs(root, 0); } - private int sumNumbers(TreeNode root, int sum) { - if (root==null) return 0; - sum = sum *10 + root.val; - if (root.left==null && root.right==null) return sum; - return sumNumbers(root.left,sum)+sumNumbers(root.right,sum); + + private int dfs(TreeNode root, int presum) { + if (root == null) { + return 0; + } + int s = presum * 10 + root.val; + if (root.left == null && root.right == null) { + return s; + } + return dfs(root.left, s) + dfs(root.right, s); } } \ No newline at end of file diff --git a/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.py b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.py new file mode 100644 index 0000000000000..db8d64c45d6a9 --- /dev/null +++ b/solution/0100-0199/0129.Sum Root to Leaf Numbers/Solution.py @@ -0,0 +1,17 @@ +# Definition for a binary tree node. +# class TreeNode: +# def __init__(self, val=0, left=None, right=None): +# self.val = val +# self.left = left +# self.right = right +class Solution: + def sumNumbers(self, root: TreeNode) -> int: + def dfs(root, presum): + if root is None: + return 0 + s = 10 * presum + root.val + if root.left is None and root.right is None: + return s + return dfs(root.left, s) + dfs(root.right, s) + + return dfs(root, 0) diff --git a/solution/0200-0299/0261.Graph Valid Tree/README.md b/solution/0200-0299/0261.Graph Valid Tree/README.md index 42d0a15961f64..c4e7eafa95aac 100644 --- a/solution/0200-0299/0261.Graph Valid Tree/README.md +++ b/solution/0200-0299/0261.Graph Valid Tree/README.md @@ -29,7 +29,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -46,7 +46,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -66,7 +66,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md index 7467e360d304e..7a5fa467d8178 100644 --- a/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md +++ b/solution/0300-0399/0323.Number of Connected Components in an Undirected Graph/README.md @@ -42,7 +42,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -60,7 +60,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -80,7 +80,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0500-0599/0547.Number of Provinces/README.md b/solution/0500-0599/0547.Number of Provinces/README.md index 744cc4e4d02c6..bb609330c28e0 100644 --- a/solution/0500-0599/0547.Number of Provinces/README.md +++ b/solution/0500-0599/0547.Number of Provinces/README.md @@ -60,7 +60,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -77,7 +77,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n # 返回x的祖宗节点 @@ -96,7 +96,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0600-0699/0684.Redundant Connection/README.md b/solution/0600-0699/0684.Redundant Connection/README.md index c0a29ca42da99..bdec9f928cbbc 100644 --- a/solution/0600-0699/0684.Redundant Connection/README.md +++ b/solution/0600-0699/0684.Redundant Connection/README.md @@ -53,7 +53,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -70,7 +70,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -90,7 +90,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0600-0699/0695.Max Area of Island/README.md b/solution/0600-0699/0695.Max Area of Island/README.md index 02ea8860e0670..61ffbb4e09ecd 100644 --- a/solution/0600-0699/0695.Max Area of Island/README.md +++ b/solution/0600-0699/0695.Max Area of Island/README.md @@ -49,7 +49,7 @@ DFS 或并查集实现。 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -66,7 +66,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -86,7 +86,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0700-0799/0737.Sentence Similarity II/README.md b/solution/0700-0799/0737.Sentence Similarity II/README.md index de82c18394343..9726f5a13a80c 100644 --- a/solution/0700-0799/0737.Sentence Similarity II/README.md +++ b/solution/0700-0799/0737.Sentence Similarity II/README.md @@ -36,7 +36,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -53,7 +53,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -73,7 +73,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0700-0799/0765.Couples Holding Hands/README.md b/solution/0700-0799/0765.Couples Holding Hands/README.md index 5a82b2219d602..a049e56028760 100644 --- a/solution/0700-0799/0765.Couples Holding Hands/README.md +++ b/solution/0700-0799/0765.Couples Holding Hands/README.md @@ -52,7 +52,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -69,7 +69,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -89,7 +89,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0800-0899/0839.Similar String Groups/README.md b/solution/0800-0899/0839.Similar String Groups/README.md index 8a9abfd9cf498..c46c5d51199da 100644 --- a/solution/0800-0899/0839.Similar String Groups/README.md +++ b/solution/0800-0899/0839.Similar String Groups/README.md @@ -56,7 +56,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -73,7 +73,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -93,7 +93,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0900-0999/0924.Minimize Malware Spread/README.md b/solution/0900-0999/0924.Minimize Malware Spread/README.md index 57ff7cc6dcca2..0be1b2f37e59f 100644 --- a/solution/0900-0999/0924.Minimize Malware Spread/README.md +++ b/solution/0900-0999/0924.Minimize Malware Spread/README.md @@ -60,7 +60,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -77,7 +77,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -97,7 +97,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0900-0999/0928.Minimize Malware Spread II/README.md b/solution/0900-0999/0928.Minimize Malware Spread II/README.md index 9c327ab2e79eb..b7acb2241dee3 100644 --- a/solution/0900-0999/0928.Minimize Malware Spread II/README.md +++ b/solution/0900-0999/0928.Minimize Malware Spread II/README.md @@ -60,7 +60,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -77,7 +77,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -97,7 +97,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/0900-0999/0990.Satisfiability of Equality Equations/README.md b/solution/0900-0999/0990.Satisfiability of Equality Equations/README.md index 9eb8fdb068e73..c405cd99647a7 100644 --- a/solution/0900-0999/0990.Satisfiability of Equality Equations/README.md +++ b/solution/0900-0999/0990.Satisfiability of Equality Equations/README.md @@ -68,7 +68,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -85,7 +85,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -105,7 +105,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md index d1028f12247d7..a60060d17f998 100644 --- a/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md +++ b/solution/1000-1099/1061.Lexicographically Smallest Equivalent String/README.md @@ -62,7 +62,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 @@ -79,7 +79,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -99,7 +99,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n diff --git a/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md b/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md index 8572bb4a41b73..77b6d350f63c1 100644 --- a/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md +++ b/solution/1300-1399/1319.Number of Operations to Make Network Connected/README.md @@ -67,7 +67,7 @@ 模板 1——朴素并查集: ```python -# 初始化,p存储每个点的祖宗节点 +# 初始化,p存储每个点的父节点 p = list(range(n)) # 返回x的祖宗节点 def find(x): @@ -82,7 +82,7 @@ p[find(a)] = find(b) 模板 2——维护 size 的并查集: ```python -# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 +# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量 p = list(range(n)) size = [1] * n @@ -102,7 +102,7 @@ if find(a) != find(b): 模板 3——维护到祖宗节点距离的并查集: ```python -# 初始化,p存储每个点的祖宗节点,d[x]存储x到p[x]的距离 +# 初始化,p存储每个点的父节点,d[x]存储x到p[x]的距离 p = list(range(n)) d = [0] * n