103. Binary Tree Zigzag Level Order Traversal

原创 2016年08月30日 19:12:05

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]
]

我们发现一个规律,奇数正序,偶数反序,所以获取到每个层次的元素就可以ac了。

/**
 * 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> > outer;
        vector<int> inner;
        queue<TreeNode*> q;
        if (root)
           q.push(root);
        else
           return outer;
        int k=1;
        while (!q.empty())
        {
            int size=q.size();
            inner.clear();
            while (size-->0)
            {
                TreeNode* t=q.front();
                q.pop();
                inner.push_back(t->val);
                if (t->left)
                   q.push(t->left);
                if (t->right)
                   q.push(t->right);
            }
            if (k%2)
               outer.push_back(inner);
            else
            {
                reverse(inner.begin(),inner.end());
                outer.push_back(inner);
            }
            k++;
        }
        return outer;
    }
};


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

Leetcode 103 ,Binary Tree Zigzag Level Order Traversal(二叉树Z形分层遍历)

一,问题描述1,1,给定一棵二叉树,从上到下,进行Z字行分层遍历的,即:本层的话从左打到右,那么下层就是从右到左,最后输出结果。2,例如: 3,解题思路: 这题和第102题解法类似,对10...

(二叉树z字形层次遍历)LeetCode#103. Binary Tree Zigzag Level Order Traversal

按照z字形层次遍历二叉树

leetcode 103. Binary Tree Zigzag Level Order Traversal-深度遍历|广度遍历|递归|非递归

原题链接:103. Binary Tree Zigzag Level Order Traversal 【思路-Java】bfs、非递归实现 要将二叉树的 zigzag 遍历返回。本思路采用 dfs...

【LeetCode】103. Binary Tree Zigzag Level Order Traversal

题目: Given a binary tree, return the zigzag level order traversal of its nodes’ values. (ie, from le...

<LeetCode OJ> 103. Binary Tree Zigzag Level Order Traversal

103. Binary Tree Zigzag Level Order Traversal My Submissions Question Total Accepted: 54617 Total...

LeetCode 103. Binary Tree Zigzag Level Order Traversal

Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from left to...

leetcode_103_Binary Tree Zigzag Level Order Traversal

思路: 具体的思路和层序遍历一致,只是需要将偶数层的结点值颠倒一下即可,时间略长,可见本方法并非好的方法。思路详见层序遍历:http://blog.csdn.net/mnmlist/article/...

[LeetCode]103.Binary Tree Zigzag Level Order Traversal

【题目】 Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from ...

103. Binary Tree Zigzag Level Order Traversal

逐行换方向层序遍历二叉树,queue和stack的不用应用。

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)