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 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]
]

confused what "{1,#,2,3}" means? >
read more on how binary tree is serialized on OJ.

题意:给定一棵二叉树,返回按zigzag层次遍历的结果

思路:

还是跟前面的Binary Tree Level Order Traversal的思路一样

即从上往下按层遍历二叉树,将每一层的节点存放到该层对应的数组中

最后将得到的总数组中奇数层(从0层开始计数)的子数组reverse一下就可以了

复杂度:时间O(n),空间O(n)

相关题目:

Binary Tree Level Order Traversal

Binary Tree Level Order Traversal II

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:

    vector<vector<int> >zigzagLevelOrder(TreeNode *root){
    	vector<vector<int> > result;
    	queue<NodeWithLevel> q;
    	q.push(NodeWithLevel(root, 0));
    	while (!q.empty())
    	{
    		NodeWithLevel cur = q.front(); q.pop();
    		TreeNode *p = cur.p;
    		if(p){
    			if(result.size() <= cur.level){
    				vector<int> tem;
    				tem.push_back(p->val);
    				result.push_back(tem);

    			}else{
    				result[cur.level].push_back(p->val);
    			}
    			NodeWithLevel left(p->left, cur.level + 1);
    			NodeWithLevel right(p->right, cur.level + 1);
    			q.push(left);
    			q.push(right);
    		}
    	}
    	for(int i = 1; i < result.size(); i += 2) reverse(result[i].begin(), result[i].end());
    	return result;
    }

private:
    struct NodeWithLevel{
    	TreeNode *p;
    	int level;
    	NodeWithLevel(TreeNode *pp, int l):p(pp), level(l){}
    };

};

Leetcode 树 Binary Tree Zigzag Level Order Traversal

时间: 2024-10-22 22:15:12

Leetcode 树 Binary Tree Zigzag Level Order Traversal的相关文章

[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 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

[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】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

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