Binary Tree Zigzag Level Order traversal--Leetcode

Source: Internet
Author: User

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

Return its zigzag level order traversal as:

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

Idea: Method One is that each layer is stored in the result from left to right, and finally reverses all odd layers (root No. 0). Time complexity O (n)

1 classSolution {2  Public:3     voidHelp (vector<vector<int> >& res, treenode* root,intdepth)4     {5         if(!root)return;6         if(Res.size () < depth +1)7Res.push_back (vector<int> (1, root->val));8         ElseRes[depth].push_back (root->val);9Help (res, root->left, depth +1);TenHelp (res, root->right, depth +1); One     } Avector<vector<int>> Zigzaglevelorder (treenode*root) { -vector<vector<int> >Res; -Help (res, root,0); the          for(inti =1, n = res.size (); I < n; i + =2) - Reverse (Res[i].begin (), Res[i].end ()); -         returnRes; -     } +};

The second method: use a queue. Traverse each layer in turn.  Use a BOOL type variable to record whether the current layer needs to be left-to-right or right-to-left. Each layer in the queue is a left-to-right traversal order, and if the current layer needs to be left-to-right, the result is stored directly; otherwise, the subscript that the node should be in is computed and deposited.

The way to traverse each layer with a queue is that at the beginning of each layer, the nodes in the current queue are all nodes of that layer, and the number of elements in the queue is the number of elements that the layer should have. If the layer needs to traverse from right to left, then the subscript for each element in the result should be size-i-1.

The time complexity of the method is also O (n).

Class Solution {public:    vector<vector<int>> zigzaglevelorder (treenode* root) {        Vector<vector <int> > Res;        if (!root) return res;        Queue<treenode *> Q;        Q.push (root);        bool LoR = true;        while (!q.empty ())        {            int size = Q.size ();            vector<int> cand (size);            for (int i = 0; i < size; i++)            {                int index = LoR? i:size-i-1;                treenode* cur = q.front ();                Q.pop ();                Cand[index] = cur->val;                if (cur->left) Q.push (cur->left);                if (cur->right) Q.push (cur->right);            }            Res.push_back (cand);            LoR =! LoR;        }        return res;    }};

  

Binary Tree Zigzag Level Order traversal--Leetcode

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.