Sword refers to Offer 6th day search and backtracking algorithm (simple)
Interview Question 32 - I. Printing a Binary Tree from Top to Bottom
Topic description
Each node of the binary tree is printed from top to bottom, and the nodes of the same layer are printed from left to right.
For example:
Given a binary tree: [3,9,20,null,null,15,7],
Returns:
[3,9,20,15,7]
ideas
This is a hierarchical traversal, a layer-by-layer traversal, so you can't use dfs, you can only use bfs, use queue, which is deque in python
code
class Solution:
def levelOrder(self, root: TreeNode) -> List[int]:
if not root :
return []
q = collections.deque()
q.append(root)
a = []
while q :
t = q.popleft()
if t.left :
q.append(t.left)
if t.right :
q.append(t.right)
a.append(t.val)
return a
The sword refers to Offer 32 - II. Print the binary tree II from top to bottom
Topic description
The binary tree is printed in layers from top to bottom, the nodes of the same layer are printed from left to right, and each layer is printed to one line.
Sample
The input description is the same as the previous topic , and the
output description is as follows:
[
[3],
[9,20],
[15,7]
]
ideas
The basic idea is similar to the previous topic, except that the way of storing the results is different.
code
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root :
return []
q = collections.deque()
res = []
q.append(root)
while q:
tmp = []
for _ in range(len(q)):
t = q.popleft()
tmp.append(t.val)
if t.left :
q.append(t.left)
if t.right :
q.append(t.right)
res.append(tmp)
return res
The sword refers to Offer 32 - III. Print the binary tree III from top to bottom
Topic description
Please implement a function to print the binary tree in zigzag order, that is, the first line is printed in left-to-right order, the second layer is printed in right-to-left order, the third line is printed in left-to-right order, and the other line and so on.
Sample
The input description is the same as the previous topic, and the
output description is as follows
[
[3],
[20,9],
[15,7]
]
ideas
The writing method is similar to the previous topic. When a two-dimensional array is stored, it is judged from left to right or from right to left according to the subscript of the number of rows.
code
c++ code
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
que.push(root);
vector<vector<int>> g;
vector<int> tmp;
if(!root)return g;
while(!que.empty()){
int len = que.size();
tmp.clear();
while(len--){
TreeNode* t = que.front();
que.pop();
tmp.push_back(t->val);
if(t->left){
que.push(t->left);
}
if(t->right){
que.push(t->right);
}
}
g.push_back(tmp);
}
for(int i=1;i<g.size();i+=2){
reverse(g[i].begin(),g[i].end());
}
return g;
}
};
python code
class Solution:
def levelOrder(self, root: TreeNode) -> List[List[int]]:
if not root :
return []
q = collections.deque()
res = []
q.append(root)
i = 0
while q:
i = i + 1
tmp = collections.deque()
for _ in range(len(q)):
t = q.popleft()
if i%2==1 :
tmp.append(t.val)
else :
tmp.appendleft(t.val)
if t.left :
q.append(t.left)
if t.right :
q.append(t.right)
res.append(list(tmp))
return res
If this article is helpful to my friends, I hope to give a like and support~ Thank you very much~