Description
Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from left to right, then right to left for the next level and alternate between).
For example:
Given binary tree [3,9,20,null,null,15,7],
3
/ \
9 20
/ \
15 7
return its zigzag level order traversal as:
[
[3],
[20,9],
[15,7]
]
分析一
题目的意思是:实现树的之字形遍历。
方法一是用双向队列,题目本身难度比较小,注意分情况讨论就行了。
代码一
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
vector<vector<int> > result;
if(root==NULL)
return result;
deque<TreeNode *> q1;
TreeNode *temp;
q1.push_back(root);
int zz=0;
while(!q1.empty()){
int len=q1.size();
vector<int> ans;
for(int i=0;i<len;i++){
if(zz%2!=0){
temp=q1.front();
ans.push_back(temp->val);
q1.pop_front();
if(temp->right){
q1.push_back(temp->right);
}
if(temp->left){
q1.push_back(temp->left);
}
}else{
temp=q1.back();
ans.push_back(temp->val);
q1.pop_back();
if(temp->left){
q1.push_front(temp->left);
}
if(temp->right){
q1.push_front(temp->right);
}
}
}
zz++;
result.push_back(ans);
}
return result;
}
};
分析二
- 比起双向队列,我觉得双栈的操作很有趣,我比较推崇第二种方法。
代码二
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
vector<vector<int>> res;
if(!root){
return res;
}
stack<TreeNode*> s1;
stack<TreeNode*> s2;
s1.push(root);
while(s1.size()||s2.size()){
vector<int> ans;
while(!s1.empty()){
TreeNode* cur=s1.top();
ans.push_back(cur->val);
s1.pop();
if(cur->left){
s2.push(cur->left);
}
if(cur->right){
s2.push(cur->right);
}
}
if(ans.size()){
res.push_back(ans);
}
ans.clear();
while(!s2.empty()){
TreeNode* cur=s2.top();
ans.push_back(cur->val);
s2.pop();
if(cur->right){
s1.push(cur->right);
}
if(cur->left){
s1.push(cur->left);
}
}
if(ans.size()){
res.push_back(ans);
}
}
return res;
}
};
python方案如下,用到了双向队列:
#
# @lc app=leetcode id=103 lang=python3
#
# [103] Binary Tree Zigzag Level Order Traversal
#
# @lc code=start
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
if not root:
return []
q = deque([root])
left_to_right = True
res = []
while q:
level_len = len(q)
values = deque()
for i in range(level_len):
cur = q.popleft()
if left_to_right:
values.append(cur.val)
else:
values.appendleft(cur.val)
if cur.left:
q.append(cur.left)
if cur.right:
q.append(cur.right)
left_to_right = not left_to_right
res.append(list(values))
return res
参考文献
[编程题]binary-tree-zigzag-level-order-traversal
[LeetCode] Binary Tree Zigzag Level Order Traversal 二叉树的之字形层序遍历
本文介绍了一种特殊的二叉树遍历方法——之字形遍历,通过使用双向队列或双栈实现从左到右再从右到左的交替遍历方式,详细解析了两种方法的代码实现过程。
9755

被折叠的 条评论
为什么被折叠?



