1. 程式人生 > >Lintcode 71 Binary Tree Zigzag Level Order Traversal

Lintcode 71 Binary Tree Zigzag Level Order Traversal

efi null new top def lint tac cnblogs ever

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).

Example

Given binary tree {3,9,20,#,#,15,7},

    3
   /   9  20
    /     15   7

return its zigzag level order traversal as:

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

Java Version 用count計數實現奇數層reverse

 1 /**
 2 
 3 Definition of TreeNode:
 4 public class TreeNode {
 5 public int val;
 6 public TreeNode left, right;
 7 public TreeNode(int val) {
 8 this.val = val;
 9 this.left = this.right = null;
10 }
11 }*/
12 public class Solution {
13 
14 /**
15  * @param root: The root of binary tree.
16 * @return: A list of lists of integer include 17 * the zigzag level order traversal of its nodes‘ values 18 */ 19 public ArrayList<ArrayList<Integer>> zigzagLevelOrder(TreeNode root) { 20 // write your code here 21 ArrayList <ArrayList<Integer>> result = new
ArrayList(); 22 23 if (root == null) { 24 return result; 25 } 26 Queue<TreeNode> queue = new LinkedList<TreeNode>(); queue.offer(root); 27 int count = 0; 28 while(!queue.isEmpty()){ 29 30 int size = queue.size(); 31 ArrayList<Integer> level = new ArrayList<Integer>(); 32 33 for(int i=0; i<size; i++){ 34 35 TreeNode head = queue.poll(); 36 level.add(head.val); 37 if(head.left!=null) queue.offer(head.left); 38 if(head.right!=null) queue.offer(head.right); 39 } 40 if(count%2 == 1) Collections.reverse(level); 41 count++; 42 result.add(level); 43 } 44 return result; 45 } 46 }

C++Version
我們用到棧的後進先出的特點,這道題我們維護兩個棧,相鄰兩行分別存到兩個棧中,進棧的順序也不相同,一個棧是先進左子結點然後右子節點,另一個棧是先進右子節點然後左子結點,這樣出棧的順序就是我們想要的之字形了

 1 /**
 2 
 3 Definition of TreeNode:
 4 class TreeNode {
 5 public:
 6 int val;
 7 TreeNode left, right;
 8 TreeNode(int val) {
 9 this->val = val;
10 this->left = this->right = NULL;
11 }
12 }*/
13 class Solution {
14 
15 /**
16  * @param root: The root of binary tree.
17  * @return: A list of lists of integer include 
18  *          the zigzag level order traversal of its nodes‘ values 
19  */
20 public:
21 
22 vector<vector<int>> zigzagLevelOrder(TreeNode *root) {
23     // write your code here
24     vector<vector<int>> res;
25     if(!root) return res;
26     
27     stack<TreeNode*> s1;
28     stack<TreeNode*> s2;
29     
30     s1.push(root);
31     vector<int> level;
32     
33     while(!s1.empty() || !s2.empty()){
34         while(!s1.empty()){
35             TreeNode *cur = s1.top();
36             s1.pop();
37             level.push_back(cur->val);
38             if(cur->left) s2.push(cur->left);
39             if(cur->right) s2.push(cur->right);
40         }
41         if(!level.empty()) 
42         res.push_back(level);
43         level.clear();
44         while(!s2.empty()){
45             TreeNode *cur = s2.top();
46             s2.pop();
47             level.push_back(cur->val);
48             if(cur->right) s1.push(cur->right);
49             if(cur->left) s1.push(cur->left);
50         }
51         if(!level.empty())
52         res.push_back(level);
53         level.clear();
54     }
55     return res;
56 }
57 };

Lintcode 71 Binary Tree Zigzag Level Order Traversal