Print binary tree in zigzag order
- Number of participants: 703 time limit: 1 seconds space limit: 32768K
- By scale: 25.31%
- Best record: 0 ms|8552k()
Topic Description Please implement a function to print the binary tree according to the glyph, that is, the first line is printed from left to right, the second layer prints from right to left, the third line prints from left to right, and so on in the other rows.
Test instructions: See Test cases
Test Case:
{8,6,10,5,7,9,11}
The corresponding output should be:
[[8],[10,6],[5,7,9,11]]
Idea: Use a Boolean to control left-to-right or right-to-left order, put it in the stack first, and then put his left and right children in the same order as from left (or right-to-left), according to the bank's reading order, and then put the data in the stack when the bank is finished. It's OK.
Links: Http://www.nowcoder.com/practice/91b69814117f4e8097390d107d2efbe0?rp=3&ru=/ta/coding-interviews&qru =/ta/coding-interviews/question-ranking
/** Topic Description Please implement a function to print the binary tree according to the glyph, that is, the first line prints from left to right, the second layer prints from right to left, the third line prints from left to right, and so on in the other rows. * * #include <cstdio> #include <stack> #include <queue> #include <vector>using namespace std; struct TreeNode {int val; struct TreeNode *left; struct TreeNode *right; TreeNode (int x): Val (x), left (null), right (null) {}}; TreeNode *root;class Solution {public:vector<vector<int> > Print (treenode* proot) {Vector<vecto R<int> >ans; if (Proot==null) return ans; Vector<int> arr; Queue<treenode*> T; Stack<treenode*> S; BOOL Bo=true; Used to record whether the current is left-to-right or right-to-left int cnt=1; S.push (Proot); while (! S.empty ()) {int tmp=0; treenode* P=s.top (); S.pop (); Arr.push_back (P->val); TreeNode *left=p->left; TreeNode *right=p->right; cnt--; if (bo) {if (Left!=nulL) {T.push (left);} if (right!=null) {T.push (right);} } else {if (right!=null) {T.push (right);} if (left!=null) {T.push (left);} } if (cnt==0)//Line end {Bo=!bo; Ans.push_back (arr); Arr.clear (); while (! T.empty ()) {treenode* q=t.front (); T.pop (); S.push (q); cnt++; }}} return ans; }}; TreeNode *creat (int *pre,int *in,int n) {TreeNode *s; for (int i=0;i<n;i++) {if (Pre[0]==in[i]) {s=new TreeNode (in[i]); The S->left=creat (pre+1,in,i) on the left side of the root node in the middle sequence of the calendar passes; On the right side of the root node are the right subtree, and the right subtree needs to start from i+1 s->right=creat (pre+i+1,in+i+1,n-i-1); return s; }} return NULL;} int main () {solution so; int n; While(scanf ("%d", &n)!=eof) {root=null; int a[2005],b[2005]; for (int i=0;i<n;i++) scanf ("%d", &a[i]); for (int i=0;i<n;i++) scanf ("%d", &b[i]); Root=creat (A,b,n); Vector<vector<int> >ans=so. Print (root); for (int i=0;i<ans.size (), i++) {for (int j=0;j<ans[i].size (); j + +) {print F ("%d", ans[i][j]); } printf ("\ n"); }} return 0;} /*78 6 5 7 10 9 115 6 7 8 9 10 11 test case: {8,6,10,5,7,9,11} The corresponding output should be: [[8],[10,6],[5,7,9,11]]*/
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Print the binary tree in the order of the glyphs (the application of the Sword offer+ team stack)