LeetCode[Tree]: 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]

]

我的迭代解法如下:

vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
    vector<vector<int> > res;

    vector<TreeNode *> iter;
    if (root) iter.push_back(root);

    int isForward = 1;
    while (!iter.empty()) {
        int size = iter.size();
        vector<int> level;

        for (int i = 0; i < size; ++i) {
            level.push_back(iter[i]->val);
            if (iter[i]->left) iter.push_back(iter[i]->left);
            if (iter[i]->right) iter.push_back(iter[i]->right);
        }

        if (!isForward) reverse(level.begin(), level.end());
        res.push_back(level);
        iter.erase(iter.begin(), iter.begin() + size);
        isForward ^= 0x01;
    }

    return res;
}

我的递归解法如下:

class Solution {
public:
    vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
        res.clear();
        if (!root) return res;
        zigzagLevelOrder(root, 1);
        return res;
    }

private:
    vector<vector<int> > res;

    void zigzagLevelOrder(TreeNode *root, int level) {
        if (res.size() < level) res.resize(level);
        if (level % 2) res[level - 1].push_back(root->val);
        else res[level - 1].insert(res[level - 1].begin(), root->val);

        if (root->left) zigzagLevelOrder(root->left, level + 1);
        if (root->right) zigzagLevelOrder(root->right, level + 1);
    }
};

两种算法的时间性能相同,如下图所示:

时间: 2024-10-15 05:20:02

LeetCode[Tree]: Binary Tree Zigzag Level Order Traversal的相关文章

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 zig

[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 @ 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

[LeetCode] 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 zigz

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

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 来源: https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Given a binary tree, return the zigzag level order traversal of its nodes' values

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】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51524241 Subject 出处:https://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 ri