LeetCode OJ: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,#,#,15,7},

    3
   /   9  20
    /     15   7

return its zigzag level order traversal as:

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

简单的bfs而已,不过在bfs的过程中应该注意将相应的数组reverse一下,其实都到最终结果之后在隔行reverse也是可以的,下面给出非递归的版本,用递归同样也很好实现:

 1 /**
 2  * Definition for a binary tree node.
 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     struct Node
12     {
13         TreeNode * node;
14         int level;
15         Node(){}
16         Node(TreeNode * n, int lv)
17         : node(n), level(lv){}
18     };
19 public:
20     vector<vector<int>> zigzagLevelOrder(TreeNode* root) {
21         vector<vector<int>> ret;
22         if(!root) return ret;
23         vector<int> tmp;
24         int dep = 0;
25         queue<Node>q;
26         q.push(Node(root, 0));
27         while(!q.empty()){
28             Node tmpNode = q.front(); //非递归的使用bfs,借助队列特性
29             if(tmpNode.node->left)
30                 q.push(Node(tmpNode.node->left, tmpNode.level + 1));
31             if(tmpNode.node->right)
32                 q.push(Node(tmpNode.node->right, tmpNode.level + 1));
33             if(dep < tmpNode.level){
34                 if(dep % 2){
35                     reverse(tmp.begin(), tmp.end());
36                 }
37                 ret.push_back(tmp);
38                 tmp.clear();
39                 dep = tmpNode.level;
40             }
41             tmp.push_back(tmpNode.node->val);
42             q.pop();
43         }
44         if(dep % 2){
45             reverse(tmp.begin(), tmp.end());
46         }
47         ret.push_back(tmp);
48         return ret;
49     }
50 };
时间: 2024-10-15 17:20:50

LeetCode OJ:Binary Tree Zigzag Level Order Traversal(折叠二叉树遍历)的相关文章

leetCode 103.Binary Tree Zigzag Level Order Traversal (二叉树Z字形水平序) 解题思路和方法

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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

Leetcode 树 Binary Tree Zigzag Level Order Traversal

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie Binary Tree Zigzag Level Order Traversal Total Accepted: 8678 Total Submissions: 33047My Submissions Given a binary tree, return the zigzag level order traversal of its nodes' values. (ie, from le

【leetcode】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,#,#,15,7}, 3 / \ 9 20 / \ 15 7return its z

[C++]LeetCode: 101 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,#,#,15,7}, 3 / 9 20 / 15 7 return its

Leetcode 103. Binary Tree Zigzag Level Order Traversal

1 /** 2 * Definition for a binary tree node. 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>>

[LeetCode][Java] 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,#,#,15,7}, 3 / 9 20 / 15 7 return its

leetcode No103. Binary Tree Zigzag Level Order Traversal

Question: 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

leetCode(23):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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

Java for LeetCode 103 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,#,#,15,7}, 3 / 9 20 / 15 7 return its zig

【leetcode】Binary Tree Zigzag Level Order Traversal (middle)

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,#,#,15,7}, 3 / 9 20 / 15 7 return its zigz