Skip to content

Commit

Permalink
feat: add python and java solutions to lcof problem
Browse files Browse the repository at this point in the history
添加《剑指 Offer》题解:面试题32 - I. 从上到下打印二叉树
  • Loading branch information
yanglbme committed Mar 9, 2020
1 parent 7853d4c commit e4ff1e4
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 0 deletions.
101 changes: 101 additions & 0 deletions lcof/面试题32 - I. 从上到下打印二叉树/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# [面试题32 - I. 从上到下打印二叉树](https://leetcode-cn.com/problems/cong-shang-dao-xia-da-yin-er-cha-shu-lcof/)

## 题目描述
从上到下打印出二叉树的每个节点,同一层的节点按照从左到右的顺序打印。

**例如:**

给定二叉树: `[3,9,20,null,null,15,7]`,

```
3
/ \
9 20
/ \
15 7
```

**返回:**

```
[3,9,20,15,7]
```

**提示:**

- `节点总数 <= 1000`

## 解法
### Python3
```python
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

from queue import Queue

class Solution:
def levelOrder(self, root: TreeNode) -> List[int]:
if root is None:
return []
s = Queue()
res = []
s.put(root)
while not s.empty():
node = s.get()
res.append(node.val)
if node.left:
s.put(node.left)
if node.right:
s.put(node.right)
return res
```

### Java
```java
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int[] levelOrder(TreeNode root) {
if (root == null) {
return new int[0];
}
Queue<TreeNode> q = new LinkedList<>();
Queue<Integer> s = new LinkedList<>();
q.offer(root);

while (!q.isEmpty()) {
TreeNode node = q.poll();
s.offer(node.val);
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
int[] res = new int[s.size()];

for (int i = 0, len = s.size(); i < len; ++i) {
res[i] = s.poll();
}
return res;

}
}
```

### ...
```
```
37 changes: 37 additions & 0 deletions lcof/面试题32 - I. 从上到下打印二叉树/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int[] levelOrder(TreeNode root) {
if (root == null) {
return new int[0];
}
Queue<TreeNode> q = new LinkedList<>();
Queue<Integer> s = new LinkedList<>();
q.offer(root);

while (!q.isEmpty()) {
TreeNode node = q.poll();
s.offer(node.val);
if (node.left != null) {
q.offer(node.left);
}
if (node.right != null) {
q.offer(node.right);
}
}
int[] res = new int[s.size()];

for (int i = 0, len = s.size(); i < len; ++i) {
res[i] = s.poll();
}
return res;

}
}
24 changes: 24 additions & 0 deletions lcof/面试题32 - I. 从上到下打印二叉树/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

from queue import Queue

class Solution:
def levelOrder(self, root: TreeNode) -> List[int]:
if root is None:
return []
s = Queue()
res = []
s.put(root)
while not s.empty():
node = s.get()
res.append(node.val)
if node.left:
s.put(node.left)
if node.right:
s.put(node.right)
return res

0 comments on commit e4ff1e4

Please sign in to comment.