37. Binary Tree zigzag level order traversal & Binary Tree inorder Traversal

Source: Internet
Author: User

Binary Tree zigzag level order traversal

Given a binary tree, returnZigzag level orderTraversal of its nodes 'values. (ie, from left to right, then right to left for the next level and alternate ).

For example: given binary tree{3, 9, 20, #, #, 15, 7},

 
3/9 20/15 7

 

Return its zigzag level order traversal:

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

Idea: Use two queues (one can be read sequentially, so we use vector to simulate it), and each queue has a layer of nodes.

/*** Definition for binary tree * struct treenode {* int val; * treenode * left; * treenode * right; * treenode (int x): Val (x ), left (null), right (null) {}*}; */void getque1 (vector <treenode *> & Q1, queue <treenode *> & Q2, vector <vector <int> & VEC) {While (! Q2.empty () {treenode * P = q2.front (); q2.pop (); If (p-> left) q1.push _ back (p-> left ); if (p-> right) q1.push _ back (p-> right);} If (q1.size () = 0) return; vector <int> vec2; for (INT I = q1.size ()-1; I> = 0; -- I) vec2.push _ back (Q1 [I]-> Val); Vec. push_back (vec2);} void getque2 (queue <treenode *> & Q2, vector <treenode *> & Q1, vector <int> & VEC) {If (q1.size () = 0) return; vector <int> vec2; For (INT I = 0; I <q1.size (); ++ I) {If (Q1 [I]-> left) {q2.push (Q1 [I]-> left ); vec2.push _ back (Q1 [I]-> left-> Val);} If (Q1 [I]-> right) {q2.push (Q1 [I]-> right ); vec2.push _ back (Q1 [I]-> right-> Val) ;}} if (vec2.size () Vec. push_back (vec2); q1.clear ();} class solution {public: vector <int> zigzaglevelorder (treenode * root) {vector <int> VEC; if (root = NULL) return VEC; queue <treenode *> Q2; vector <treenod E *> q1; q2.push (Root); Vec. push_back (vector <int> (1, root-> Val); While (! Q2.empty () {getque1 (Q1, q2, VEC); getque2 (Q2, Q1, VEC) ;}return VEC ;}};

 

Binary Tree inorder Traversal

OJ: https://oj.leetcode.com/problems/binary-tree-inorder-traversal/

Given a binary tree, returnInorderTraversal of its nodes 'values.

For example: given binary tree{1, #, 2, 3},

 
1 2/3

 

Return[1, 3, 2].

Note:Recursive solution is trivial, cocould You Do It iteratively?

Question: two methods: 1. STACK: O (n) time, O (n) space. 2. Morris traversal (construct the clue tree), O (n) time, O (1) space.

1. Use stacks

/*** 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 <int> inordertraversal (treenode * root) {vector <int> VEC; if (root = NULL) return VEC; treenode * P = root; stack <treenode *> st; ST. push (p); While (p-> left) {P = p-> left; ST. push (p) ;}while (! St. empty () {treenode * q = ST. top (); ST. pop (); Vec. push_back (Q-> Val); If (Q-> right) {q = Q-> right; ST. push (Q); While (Q-> left) {q = Q-> left; ST. push (q) ;}}return VEC ;}};

2. Morris Traversal

/*** 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 <int> inordertraversal (treenode * root) {vector <int> VEC; treenode * cur, * pre; cur = root; while (cur) {If (cur-> left = NULL) {Vec. push_back (cur-> Val); cur = cur-> right;} else {pre = cur-> left; while (pre-> Right & pr E-> right! = Cur) Pre = pre-> right; if (pre-> right = NULL) {pre-> right = cur; cur = cur-> left ;} else {pre-> right = NULL; Vec. push_back (cur-> Val); cur = cur-> right ;}}return VEC ;}};

 

37. Binary Tree zigzag level order traversal & Binary Tree inorder Traversal

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.