Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add python and java solutions to lcci problem: No.04.10 and No.04.12 #278

Merged
merged 6 commits into from Jul 20, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 37 additions & 3 deletions lcci/04.10.Check SubTree/README.md
Original file line number Diff line number Diff line change
@@ -29,20 +29,54 @@

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

先找 t1 中 t2 结点,找到后进行 DFS,确认子树和 t2 的子树完全相同,否则返回 FALSE。

### Python3
<!-- 这里可写当前语言的特殊实现逻辑 -->

```python

class Solution:
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
if t1 == None:
return False
if t2 == None:
return True
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)

def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
if not t1 and t2 :
return False
if not t2 and not t1:
return True
if t1.val != t2.val:
return False
else:
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
```

### Java
<!-- 这里可写当前语言的特殊实现逻辑 -->

```java

class Solution {
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
if (t2 == null)
return true;
if (t1 == null)
return false;
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
}

public boolean isSubTree(TreeNode t1, TreeNode t2){
if (t2 == null)
return true;
if (t1 == null)
return false;
if (t1.val != t2.val)
return false;
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
}
}
```

### ...
40 changes: 37 additions & 3 deletions lcci/04.10.Check SubTree/README_EN.md
Original file line number Diff line number Diff line change
@@ -51,18 +51,52 @@


## Solutions

Find the t2 node in t1 first, then use the depth-first search (DFS) algorithm to make sure that the subtree and the subtree of t2 are identical, otherwise return FALSE.

### Python3

```python

class Solution:
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
if t1 == None:
return False
if t2 == None:
return True
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)

def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
if not t1 and t2 :
return False
if not t2 and not t1:
return True
if t1.val != t2.val:
return False
else:
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
```

### Java

```java

class Solution {
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
if (t2 == null)
return true;
if (t1 == null)
return false;
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
}

public boolean isSubTree(TreeNode t1, TreeNode t2){
if (t2 == null)
return true;
if (t1 == null)
return false;
if (t1.val != t2.val)
return false;
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
}
}
```

### ...
19 changes: 19 additions & 0 deletions lcci/04.10.Check SubTree/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution {
public boolean checkSubTree(TreeNode t1, TreeNode t2) {
if (t2 == null)
return true;
if (t1 == null)
return false;
return isSubTree(t1, t2) || checkSubTree(t1.left, t2) || checkSubTree(t1.right, t2);
}

public boolean isSubTree(TreeNode t1, TreeNode t2){
if (t2 == null)
return true;
if (t1 == null)
return false;
if (t1.val != t2.val)
return false;
return isSubTree(t1.left,t2.left) && isSubTree(t1.right,t2.right);
}
}
17 changes: 17 additions & 0 deletions lcci/04.10.Check SubTree/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def checkSubTree(self, t1: TreeNode, t2: TreeNode) -> bool:
if t1 == None:
return False
if t2 == None:
return True
return self.dfs(t1,t2) or self.checkSubTree(t1.left,t2) or self.checkSubTree(t1.right,t2)

def dfs(self, t1: TreeNode, t2: TreeNode) -> bool:
if not t1 and t2 :
return False
if not t2 and not t1:
return True
if t1.val != t2.val:
return False
else:
return self.dfs(t1.left,t2.left) and self.dfs(t1.right,t2.right)
59 changes: 56 additions & 3 deletions lcci/04.12.Paths with Sum/README.md
Original file line number Diff line number Diff line change
@@ -32,20 +32,73 @@

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

DFS 深度优先搜索

### Python3
<!-- 这里可写当前语言的特殊实现逻辑 -->
采用递归的思想,每递归到某个节点时:
- 若`root.val-sum == 0`,结果加 1
- 考虑将此节点纳入或不纳入路径两种情况

```python
特殊情况:若此节点的父节点在路径中,此节点必纳入路径(路径不能断)

```python
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:
def dfs(root, sum, flag):
nonlocal ans
if not root:
return 0
if sum-root.val == 0:
ans += 1
if flag == 0:
dfs(root.left, sum, 0)
dfs(root.right, sum, 0)
dfs(root.left, sum-root.val, 1)
dfs(root.right, sum-root.val, 1)

if not root:
return 0
ans = 0
dfs(root, sum, 0)
return ans
```

### Java
<!-- 这里可写当前语言的特殊实现逻辑 -->
使用到2个递归过程:

```java
- BFS:(traverse)遍历每个树节点;
- DFS: 从每个树节点出发,节点求和,看是否能满足 sum。

需要注意,节点值有正有负,需要穷尽所有的可能路径。

```java
class Solution {
int ans = 0;
public int pathSum(TreeNode root, int sum) {
traverse(root, sum);
return ans;
}

void traverse(TreeNode root, int sum) {
if (root == null) return;
ans += dfs(root, sum, 0);
traverse(root.left, sum);
traverse(root.right, sum);
}

// check if sum of path is sum.
int dfs(TreeNode root, int sum, int cur) {
if (root == null) return 0;
cur += root.val;
int res = 0;
if (cur == sum) res++;
res += dfs(root.left, sum, cur);
res += dfs(root.right, sum, cur);
return res;
}
}
```

### ...
59 changes: 56 additions & 3 deletions lcci/04.12.Paths with Sum/README_EN.md
Original file line number Diff line number Diff line change
@@ -57,18 +57,71 @@ Given the following tree and &nbsp;<code>sum = 22,</code></p>


## Solutions

Depth-First-Search

### Python3
Using the idea of recursion, at each recursion to a node.
- If root.val-sum == 0, add 1 to the result
- Consider two scenarios for inclusion or exclusion of this node from the pathway

```python
Special case: if the parent node of this node is in the path, this node must be included in the path (the path cannot be broken)

```python
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:
def dfs(root, sum, flag):
nonlocal ans
if not root:
return 0
if sum-root.val == 0:
ans += 1
if flag == 0:
dfs(root.left, sum, 0)
dfs(root.right, sum, 0)
dfs(root.left, sum-root.val, 1)
dfs(root.right, sum-root.val, 1)

if not root:
return 0
ans = 0
dfs(root, sum, 0)
return ans
```

### Java
Use to 2 recursive processes.

```java
- BFS: (traverse) traverses each tree node.
- DFS: Starting from each tree node, the nodes sum to see if sum can be satisfied.

Note that node values can be positive or negative, and all possible paths need to be exhausted.

```java
class Solution {
int ans = 0;
public int pathSum(TreeNode root, int sum) {
traverse(root, sum);
return ans;
}

void traverse(TreeNode root, int sum) {
if (root == null) return;
ans += dfs(root, sum, 0);
traverse(root.left, sum);
traverse(root.right, sum);
}

// check if sum of path is sum.
int dfs(TreeNode root, int sum, int cur) {
if (root == null) return 0;
cur += root.val;
int res = 0;
if (cur == sum) res++;
res += dfs(root.left, sum, cur);
res += dfs(root.right, sum, cur);
return res;
}
}
```

### ...
25 changes: 25 additions & 0 deletions lcci/04.12.Paths with Sum/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Solution {
int ans = 0;
public int pathSum(TreeNode root, int sum) {
traverse(root, sum);
return ans;
}

void traverse(TreeNode root, int sum) {
if (root == null) return;
ans += dfs(root, sum, 0);
traverse(root.left, sum);
traverse(root.right, sum);
}

// check if sum of path is sum.
int dfs(TreeNode root, int sum, int cur) {
if (root == null) return 0;
cur += root.val;
int res = 0;
if (cur == sum) res++;
res += dfs(root.left, sum, cur);
res += dfs(root.right, sum, cur);
return res;
}
}
19 changes: 19 additions & 0 deletions lcci/04.12.Paths with Sum/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> int:
def dfs(root, sum, flag):
nonlocal ans
if not root:
return 0
if sum-root.val == 0:
ans += 1
if flag == 0:
dfs(root.left, sum, 0)
dfs(root.right, sum, 0)
dfs(root.left, sum-root.val, 1)
dfs(root.right, sum-root.val, 1)

if not root:
return 0
ans = 0
dfs(root, sum, 0)
return ans