add #32.1 #32.2 python implement
This commit is contained in:
parent
9102a94ed0
commit
28366d36b4
155
notes/面试总结.md
155
notes/面试总结.md
@ -34,6 +34,8 @@
|
||||
* [29. 顺时针打印矩阵](#29-顺时针打印矩阵)
|
||||
* [30. 包含 min 函数的栈](#30-包含-min-函数的栈)
|
||||
* [31. 栈的压入、弹出序列](#31-栈的压入弹出序列)
|
||||
* [32.1 从上往下打印二叉树](#321-从上往下打印二叉树)
|
||||
* [32.2 把二叉树打印成多行](#322-把二叉树打印成多行)
|
||||
|
||||
* [参考文献](#参考文献)
|
||||
<!-- GFM-TOC -->
|
||||
@ -2291,6 +2293,159 @@ class Solution:
|
||||
return False
|
||||
return True
|
||||
```
|
||||
# 32.1 从上往下打印二叉树
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/7fe2212963db4790b57431d9ed259701?tpId=13&tqId=11175&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
|
||||
|
||||
例如,以下二叉树层次遍历的结果为:1,2,3,4,5,6,7
|
||||
|
||||
<div align="center"> <img src="../pics//348bc2db-582e-4aca-9f88-38c40e9a0e69.png" width="250"/> </div><br>
|
||||
|
||||
## 解题思路
|
||||
|
||||
使用队列来进行层次遍历。
|
||||
|
||||
不需要使用两个队列分别存储当前层的节点和下一层的节点,因为在开始遍历一层的节点时,当前队列中的节点数就是当前层的节点数,只要控制遍历这么多节点数,就能保证这次遍历的都是当前层的节点。
|
||||
|
||||
```java
|
||||
public ArrayList<Integer> PrintFromTopToBottom(TreeNode root) {
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
ArrayList<Integer> ret = new ArrayList<>();
|
||||
queue.add(root);
|
||||
while (!queue.isEmpty()) {
|
||||
int cnt = queue.size();
|
||||
while (cnt-- > 0) {
|
||||
TreeNode t = queue.poll();
|
||||
if (t == null)
|
||||
continue;
|
||||
ret.add(t.val);
|
||||
queue.add(t.left);
|
||||
queue.add(t.right);
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
class Solution:
|
||||
# 返回从上到下每个节点值列表,例:[1,2,3]
|
||||
def PrintFromTopToBottom(self, root):
|
||||
# write code here
|
||||
queue = []
|
||||
ret = []
|
||||
queue.append(root)
|
||||
while queue:
|
||||
cnt = len(queue)
|
||||
while cnt > 0:
|
||||
cnt -= 1
|
||||
t = queue.pop(0)
|
||||
if t == None:
|
||||
continue
|
||||
ret.append(t.val)
|
||||
queue.append(t.left)
|
||||
queue.append(t.right)
|
||||
return ret
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
class Solution:
|
||||
# 返回从上到下每个节点值列表,例:[1,2,3]
|
||||
def PrintFromTopToBottom(self, root):
|
||||
# write code here
|
||||
l=[]
|
||||
if not root:
|
||||
return []
|
||||
q=[root]
|
||||
while len(q):
|
||||
t=q.pop(0)
|
||||
l.append(t.val)
|
||||
if t.left:
|
||||
q.append(t.left)
|
||||
if t.right:
|
||||
q.append(t.right)
|
||||
return l
|
||||
```
|
||||
|
||||
# 32.2 把二叉树打印成多行
|
||||
|
||||
[NowCoder](https://www.nowcoder.com/practice/445c44d982d04483b04a54f298796288?tpId=13&tqId=11213&tPage=1&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking)
|
||||
|
||||
## 题目描述
|
||||
|
||||
和上题几乎一样。
|
||||
|
||||
## 解题思路
|
||||
|
||||
```java
|
||||
ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
|
||||
ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
|
||||
Queue<TreeNode> queue = new LinkedList<>();
|
||||
queue.add(pRoot);
|
||||
while (!queue.isEmpty()) {
|
||||
ArrayList<Integer> list = new ArrayList<>();
|
||||
int cnt = queue.size();
|
||||
while (cnt-- > 0) {
|
||||
TreeNode node = queue.poll();
|
||||
if (node == null)
|
||||
continue;
|
||||
list.add(node.val);
|
||||
queue.add(node.left);
|
||||
queue.add(node.right);
|
||||
}
|
||||
if (list.size() != 0)
|
||||
ret.add(list);
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
```
|
||||
|
||||
```python
|
||||
# -*- coding:utf-8 -*-
|
||||
# class TreeNode:
|
||||
# def __init__(self, x):
|
||||
# self.val = x
|
||||
# self.left = None
|
||||
# self.right = None
|
||||
class Solution:
|
||||
# 返回二维列表[[1,2],[4,5]]
|
||||
def Print(self, pRoot):
|
||||
# write code here
|
||||
ret = []
|
||||
# queue = []
|
||||
# queue.append(pRoot)
|
||||
queue = [pRoot]
|
||||
while queue:
|
||||
list = []
|
||||
cnt = len(queue)
|
||||
while cnt > 0:
|
||||
cnt -= 1
|
||||
node = queue.pop(0)
|
||||
if node == None:
|
||||
continue
|
||||
list.append(node.val)
|
||||
queue.append(node.left)
|
||||
queue.append(node.right)
|
||||
if len(list):
|
||||
ret.append(list)
|
||||
return ret
|
||||
```
|
||||
|
||||
# 参考文献
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user