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

==========

按z字输出树节点.

思路:

leetcode上的这种题,都是一个套路,利用BFS遍历方式

要有一个队列queue<TreeNode*> q;保存下一层要访问的节点,

需要一个curr/next计数遍历,curr记录当前层已经访问了几个节点,next记录下一层要访问的节点数量

每访问完一层,需要更新curr和next值,

当访问层数level%2==0时,记录路径反转

=======

code:

/**
 * 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>> re;
        vector<int> path;
        if(root==nullptr) return re;
        help_zig(root,re,path);

        for(auto i:re){
            for(auto j:i){
                cout<<j<<" ";
            }cout<<endl;
        }cout<<endl;
        return re;
    }
    void help_zig(TreeNode *root,vector<vector<int>> &re,vector<int> &path){
        queue<TreeNode*> q;
        int curr = 1;
        int next = 0;
        q.push(root);
        int level = 0;
        while(!q.empty()){
            if(curr>0){
                TreeNode *tmp = q.front();
                path.push_back(tmp->val);
                q.pop();
                curr--;
                if(tmp->left!=nullptr){
                    q.push(tmp->left);
                    next++;
                }
                if(tmp->right!=nullptr){
                    q.push(tmp->right);
                    next++;
                }
            }else{
                curr = next;
                next = 0;
                if(level%2!=0){
                    reverse(path.begin(),path.end());
                }
                re.push_back(path);
                path.clear();
                level++;
            }
        }///while
        if(level%2!=0) reverse(path.begin(),path.end());
        re.push_back(path);
    }
};
时间: 2024-10-24 16:15:15

103. Binary Tree Zigzag Level Order Traversal的相关文章

【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

103. Binary Tree Zigzag Level Order Traversal (Tree, Queue; 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,null,null,15,7], 3   / \  9  20    /  \  

Leetcode 103. Binary Tree Zigzag Level Order Traversal

1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 public: 12 vector<vector<int>>

leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法

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

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

Java for 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 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】#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

[leetcode tree]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 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 it

[LeetCode] 103. Binary Tree Zigzag Level Order Traversal 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,null,null,15,7], 3 / 9 20 / 15 7 retur