Skip to content

Commit

Permalink
feat: add solutions to lc/lcof2 problem:Sum Root to Leaf Numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
yanglbme committed Aug 30, 2021
1 parent a7390b3 commit 999a643
Show file tree
Hide file tree
Showing 27 changed files with 537 additions and 96 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,126 @@

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

DFS。

<!-- tabs:start -->

### **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);
}
};
```

<!-- tabs:end -->
### **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)
}
```
Original file line number Diff line number Diff line change
@@ -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);
}
};
Original file line number Diff line number Diff line change
@@ -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)
}
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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)
6 changes: 3 additions & 3 deletions lcof2/剑指 Offer II 105. 岛屿的最大面积/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ DFS 或并查集实现。
模板 1——朴素并查集:

```python
# 初始化,p存储每个点的祖宗节点
# 初始化,p存储每个点的父节点
p = list(range(n))

# 返回x的祖宗节点
Expand All @@ -70,7 +70,7 @@ p[find(a)] = find(b)
模板 2——维护 size 的并查集:

```python
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
p = list(range(n))
size = [1] * n

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lcof2/剑指 Offer II 116. 朋友圈/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
模板 1——朴素并查集:

```python
# 初始化,p存储每个点的祖宗节点
# 初始化,p存储每个点的父节点
p = list(range(n))

# 返回x的祖宗节点
Expand All @@ -77,7 +77,7 @@ p[find(a)] = find(b)
模板 2——维护 size 的并查集:

```python
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
p = list(range(n))
size = [1] * n

Expand All @@ -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

Expand Down
6 changes: 3 additions & 3 deletions lcof2/剑指 Offer II 118. 多余的边/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
模板 1——朴素并查集:

```python
# 初始化,p存储每个点的祖宗节点
# 初始化,p存储每个点的父节点
p = list(range(n))

# 返回x的祖宗节点
Expand All @@ -74,7 +74,7 @@ p[find(a)] = find(b)
模板 2——维护 size 的并查集:

```python
# 初始化,p存储每个点的祖宗节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
# 初始化,p存储每个点的父节点,size只有当节点是祖宗节点时才有意义,表示祖宗节点所在集合中,点的数量
p = list(range(n))
size = [1] * n

Expand All @@ -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

Expand Down
Loading

0 comments on commit 999a643

Please sign in to comment.