LeetCode 103. Binary Tree Zigzag Level Order Traversal

原题链接在这里:https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

题目:

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,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7

return its zigzag level order traversal as:

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

题解:

这道题是BFS的变形,与Binary Tree Level Order Traversal相似。但是要求偶数行从左到右,奇数行从右到左。

其实还是BFS, 只不过需要添加一个flag来表明是否需要reverse, 这里用boolean reverse 表示.

Time Complexity: O(n).

Space: O(n). que最多有n/2个节点。

AC Java:

 1 /**
 2  * Definition for a binary tree node.
 3  * public class TreeNode {
 4  *     int val;
 5  *     TreeNode left;
 6  *     TreeNode right;
 7  *     TreeNode(int x) { val = x; }
 8  * }
 9  */
10 class Solution {
11     public List<List<Integer>> zigzagLevelOrder(TreeNode root) {
12         List<List<Integer>> res = new ArrayList<>();
13         if(root == null){
14             return res;
15         }
16         
17         LinkedList<TreeNode> que = new LinkedList<>();
18         que.add(root);
19         boolean reverse = false;
20         
21         while(!que.isEmpty()){
22             int size = que.size();
23             List<Integer> item = new ArrayList<>();
24             while(size-- > 0){
25                 TreeNode cur = que.poll();
26                 item.add(cur.val);
27                 if(cur.left != null){
28                     que.add(cur.left);
29                 }
30                 
31                 if(cur.right != null){
32                     que.add(cur.right);
33                 }
34             }
35             
36             if(reverse){
37                Collections.reverse(item); 
38             }
39             
40             res.add(item);
41             reverse = !reverse;
42         }
43         
44         return res;
45     }
46 }

AC C++:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 8  *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 9  *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
10  * };
11  */
12 class Solution {
13 public:
14     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
15         if(!root){
16             return {};
17         }
18 
19         vector<vector<int>> res;
20         deque<TreeNode*> que;
21         que.push_back(root);
22         bool shouldReverse = false;
23         while(!que.empty()){
24             vector<int> item;
25             int size = que.size();
26             while(size-- > 0){
27                 TreeNode* cur = que.front();
28                 que.pop_front();
29                 item.push_back(cur->val);
30 
31                 if(cur->left){
32                     que.push_back(cur->left);
33                 }
34 
35                 if(cur->right){
36                     que.push_back(cur->right);
37                 }
38             }
39 
40             if(shouldReverse){
41                 reverse(item.begin(), item.end());
42             }
43 
44             shouldReverse = !shouldReverse;
45             res.push_back(item);
46         }
47 
48         return res;
49     }
50 };

AC Python:

 1 # Definition for a binary tree node.
 2 # class TreeNode:
 3 #     def __init__(self, val=0, left=None, right=None):
 4 #         self.val = val
 5 #         self.left = left
 6 #         self.right = right
 7 class Solution:
 8     def zigzagLevelOrder(self, root: Optional[TreeNode]) -> List[List[int]]:
 9         if not root:
10             return []
11         
12         res = []
13         que = deque([root])
14         should_reverse = False
15 
16         while que:
17             item = []
18             size = len(que)
19 
20             for _ in range(size):
21                 cur = que.popleft()
22                 item.append(cur.val)
23 
24                 if cur.left:
25                     que.append(cur.left)
26                 if cur.right:
27                     que.append(cur.right)
28 
29             if should_reverse:
30                 item.reverse()
31 
32             should_reverse = not should_reverse
33             res.append(item)
34 
35         return res
36 
37         

 

posted @ 2015-08-21 07:48  Dylan_Java_NYC  阅读(261)  评论(0编辑  收藏  举报