forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add python and java solutions to lcof problem
添加《剑指 Offer》题解:面试题32 - I. 从上到下打印二叉树
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} | ||
``` | ||
|
||
### ... | ||
``` | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |