Skip to content

Commit

Permalink
feat: Add python code for problem 102, 113 (azl397985856#222)
Browse files Browse the repository at this point in the history
* Fix solution on problem 88 with js, fixs azl397985856#62

* Fix solution on problem 88, fixs azl397985856#62

* Python solution for problem 90

* Add python solution for problem 92

* Add python solution for problem 94

* Add python code for problem 98

* add python code for problem 102

* add python code for problem 113
  • Loading branch information
kant-li authored and azl397985856 committed Nov 1, 2019
1 parent a3776ef commit ad0fadf
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 3 deletions.
38 changes: 36 additions & 2 deletions problems/102.binary-tree-level-order-traversal.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ return its level order traversal as:

如果不入队特殊元素Null来表示每层的结束,则在while循环开始时保存当前队列的长度,以保证每次只遍历一层(参考下面的C++ Code)。

> 如果采用递归方式,则需要将当前节点,当前所在的level以及结果数组传递给递归函数。在递归函数中,取出节点的值,添加到level参数对应结果数组元素中(参考下面的C++ Code)。
> 如果采用递归方式,则需要将当前节点,当前所在的level以及结果数组传递给递归函数。在递归函数中,取出节点的值,添加到level参数对应结果数组元素中(参考下面的C++ Code 或 Python Code)。

## 关键点解析
Expand All @@ -47,7 +47,7 @@ return its level order traversal as:


## 代码
* 语言支持:JS,C++
* 语言支持:JS,C++,Python3

Javascript Code:
```js
Expand Down Expand Up @@ -188,6 +188,40 @@ private:
}
};
```
Python Code:
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
"""递归法"""
if root is None:
return []
result = []
def add_to_result(level, node):
"""递归函数
:param level int 当前在二叉树的层次
:param node TreeNode 当前节点
"""
if level > len(result) - 1:
result.append([])
result[level].append(node.val)
if node.left:
add_to_result(level+1, node.left)
if node.right:
add_to_result(level+1, node.right)
add_to_result(0, root)
return result
```

## 相关题目
- [103.binary-tree-zigzag-level-order-traversal](./103.binary-tree-zigzag-level-order-traversal.md)
Expand Down
33 changes: 32 additions & 1 deletion problems/113.path-sum-ii.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ Return:

## 代码

* 语言支持:JS,C++
* 语言支持:JS,C++,Python3

JavaScript Code:

Expand Down Expand Up @@ -122,6 +122,37 @@ private:
}
};
```
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def pathSum(self, root: TreeNode, sum: int) -> List[List[int]]:
if not root:
return []
result = []
def trace_node(pre_list, left_sum, node):
new_list = pre_list.copy()
new_list.append(node.val)
if not node.left and not node.right:
# 这个判断可以和上面的合并,但分开写会快几毫秒,可以省去一些不必要的判断
if left_sum == node.val:
result.append(new_list)
else:
if node.left:
trace_node(new_list, left_sum-node.val, node.left)
if node.right:
trace_node(new_list, left_sum-node.val, node.right)
trace_node([], sum, root)
return result
```
## 相关题目

- [39.combination-sum](./39.combination-sum.md)
Expand Down

0 comments on commit ad0fadf

Please sign in to comment.