leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal

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,#,#,15,7},

    3
   /   9  20
    /     15   7

return its zigzag level order traversal as:

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

题目要求,按照zigzag的顺序访问树。这道题算是树的层序遍历的一个变种。

回想之前写树的层序遍历时,利用队列来辅助操作,访问当前节点的同时,将树的左右孩子push到队列当中,这样当上一次节点访问完成后,下一层节点已经全部按照顺序加入到了队列当中。若要以zigzag的形式访问,当上一层访问完成后,下一层的节点要逆序访问。要达到这个目的,我们自然而然会想到先进后出的栈,访问上一层节点时将其左右孩子入栈,然后再按照出栈的顺序访问下一层节点,得到的顺序正好和上一次节点访问顺序相反。当然,一个栈是不够用的,否则孩子节点压入栈后,覆盖了尚未访问的上一层节点,就成了树的先序遍历了。

程序中使用两个栈s1,s2来辅助操作,s1中储存奇数层(从1开始计算)的节点,奇数层的遍历顺序是从左到右,所以节点应该以从右向左的顺序压入栈s1中,s2中储存偶数层(从1开始计算)的节点,偶数层的遍历顺序是从右向左,所以节点应该以从左到右的顺序压入栈s2中,体现在程序上,就是节点右孩子先于左孩子压入栈s1中,节点左孩子先于右孩子压入栈s2中,只要初始顺序正确,下面s1,s2来回倒腾,顺序是定死的。

AC code:

/**
 * 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<int> temp;
        vector<vector<int>> ret;
        if(root==NULL)
            return ret;
        stack<TreeNode*> s1,s2;
        s1.push(root);
        while(!s1.empty()||!s2.empty())
        {
            if(!s1.empty())
            {
                temp.clear();
                while(!s1.empty())
                {
                    root=s1.top();
                    temp.push_back(root->val);
                    if(root->left!=NULL)
                        s2.push(root->left);
                    if(root->right!=NULL)
                        s2.push(root->right);
                    s1.pop();
                }
                ret.push_back(temp);
            }
            if(!s2.empty())
            {
                temp.clear();
                while(!s2.empty())
                {
                    root=s2.top();
                    temp.push_back(root->val);
                    if(root->right!=NULL)
                        s1.push(root->right);
                    if(root->left!=NULL)
                        s1.push(root->left);
                    s2.pop();
                }
                ret.push_back(temp);
            }
        }
    }
};

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal,布布扣,bubuko.com

时间: 2024-12-18 14:32:23

leetcode 刷题之路 63 Binary Tree Zigzag Level Order Traversal的相关文章

leetcode 刷题之路 78 Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 非递归方式实现二叉树的后序遍历. 思路:使用栈辅助实现,首先将根节点入栈,然后以栈是否为

leetcode 刷题之路 79 Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 非递归实现树的前序遍历.前序遍历比较简单,注意入栈的时候应该按照右孩子节点在前的顺序,这样

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ 题意: 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

Binary Tree ZigZag Level Order Traversal leetcode java

题目: 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,#,#,15,7}, 3 / 9 20 / 15 7 return its

[leetcode]Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal 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,#

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. class Solution { public: int maxDepth(TreeNode *root) { inM

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

Leetcode 树 Binary Tree Zigzag Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Zigzag Level Order Traversal Total Accepted: 8678 Total Submissions: 33047My Submissions Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from le

LeetCode 103. 二叉树的锯齿形层次遍历(Binary Tree Zigzag Level Order Traversal)

103. 二叉树的锯齿形层次遍历 103. Binary Tree Zigzag Level Order Traversal 题目描述 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). LeetCode103. Binary Tree Zigzag Level Order Traversal中等 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 / 9 20 / 15 7 返回锯齿形层次遍历如下: [