mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-11 04:54:51 +08:00
Merge pull request #712 from ironartisan/master
添加0102.二叉树的层序遍历递归解法Python代码
This commit is contained in:
@ -87,7 +87,9 @@ public:
|
|||||||
|
|
||||||
python代码:
|
python代码:
|
||||||
|
|
||||||
|
|
||||||
```python3
|
```python3
|
||||||
|
|
||||||
class Solution:
|
class Solution:
|
||||||
"""二叉树层序遍历迭代解法"""
|
"""二叉树层序遍历迭代解法"""
|
||||||
|
|
||||||
@ -113,7 +115,20 @@ class Solution:
|
|||||||
|
|
||||||
return results
|
return results
|
||||||
```
|
```
|
||||||
|
```python
|
||||||
|
# 递归法
|
||||||
|
class Solution:
|
||||||
|
def levelOrder(self, root: TreeNode) -> List[List[int]]:
|
||||||
|
res = []
|
||||||
|
def helper(root, depth):
|
||||||
|
if not root: return []
|
||||||
|
if len(res) == depth: res.append([]) # start the current depth
|
||||||
|
res[depth].append(root.val) # fulfil the current depth
|
||||||
|
if root.left: helper(root.left, depth + 1) # process child nodes for the next depth
|
||||||
|
if root.right: helper(root.right, depth + 1)
|
||||||
|
helper(root, 0)
|
||||||
|
return res
|
||||||
|
```
|
||||||
java:
|
java:
|
||||||
|
|
||||||
```Java
|
```Java
|
||||||
|
@ -204,7 +204,27 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```java
|
||||||
|
class Solution {
|
||||||
|
// 迭代法
|
||||||
|
public int countNodes(TreeNode root) {
|
||||||
|
if (root == null) return 0;
|
||||||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
|
queue.offer(root);
|
||||||
|
int result = 0;
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
while (size -- > 0) {
|
||||||
|
TreeNode cur = queue.poll();
|
||||||
|
result++;
|
||||||
|
if (cur.left != null) queue.offer(cur.left);
|
||||||
|
if (cur.right != null) queue.offer(cur.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
```java
|
```java
|
||||||
class Solution {
|
class Solution {
|
||||||
/**
|
/**
|
||||||
|
@ -201,7 +201,31 @@ class Solution {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
```java
|
||||||
|
// 层序遍历迭代法
|
||||||
|
class Solution {
|
||||||
|
public int sumOfLeftLeaves(TreeNode root) {
|
||||||
|
int sum = 0;
|
||||||
|
if (root == null) return 0;
|
||||||
|
Queue<TreeNode> queue = new LinkedList<>();
|
||||||
|
queue.offer(root);
|
||||||
|
while (!queue.isEmpty()) {
|
||||||
|
int size = queue.size();
|
||||||
|
while (size -- > 0) {
|
||||||
|
TreeNode node = queue.poll();
|
||||||
|
if (node.left != null) { // 左节点不为空
|
||||||
|
queue.offer(node.left);
|
||||||
|
if (node.left.left == null && node.left.right == null){ // 左叶子节点
|
||||||
|
sum += node.left.val;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (node.right != null) queue.offer(node.right);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
## Python
|
## Python
|
||||||
|
Reference in New Issue
Block a user