zoukankan      html  css  js  c++  java
  • LeetCode Binary Tree Zigzag Level Order Traversal

     //应该用两个栈  16ms
    1
    /** 2 * Definition for binary tree 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> > zigzagLevelOrder(TreeNode *root) { 13 // Start typing your C/C++ solution below 14 // DO NOT write int main() function 15 vector<int> v; 16 vector<vector<int> > re; 17 stack<TreeNode >s; 18 deque<TreeNode >q; 19 TreeNode t(0); 20 if(root==NULL) 21 { 22 return re; 23 } 24 q.push_back(*root); 25 while(!q.empty()) 26 { 27 v.clear(); 28 while(!q.empty()) 29 { 30 t=q.front(); 31 v.push_back(t.val); 32 q.pop_front(); 33 34 if(t.left) 35 s.push(*(t.left)); 36 if(t.right) 37 s.push(*(t.right)); 38 39 } 40 re.push_back(v); 41 v.clear(); 42 while(!s.empty()) 43 { 44 t=s.top(); 45 v.push_back(t.val); 46 s.pop(); 47 48 if(t.right) 49 q.push_front(*(t.right)); 50 if(t.left) 51 q.push_front(*(t.left)); 52 53 } 54 if(v.size()) 55 re.push_back(v); 56 v.clear(); 57 } 58 return re; 59 } 60 61 62 63 64 65 66 67 68 69 };
  • 相关阅读:
    day38_css
    day39_css_浮动_display
    day36_html
    线段树模板2 洛谷p3373
    hdu1257 最少拦截系统
    树的重心(DFS)
    树的直径(BFS)
    面向对象复习
    面向对象练习题
    面向对象的交互
  • 原文地址:https://www.cnblogs.com/mengqingzhong/p/3115982.html
Copyright © 2011-2022 走看看