leetcode_103题——Binary Tree Zigzag Level Order Traversal(广度优先搜索,队列queue,栈stack)

Binary Tree Zigzag Level Order Traversal

Total Accepted: 31183 Total Submissions: 117840My Submissions

Question Solution

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.

Hide Tags

Tree Breadth-first Search Stack

Have you met this question in a real interview?

Yes

No

Discuss

#include<iostream>
#include<queue>
#include<stack>
using namespace std;

 struct TreeNode {
	     int val;
	     TreeNode *left;
	     TreeNode *right;
	     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
	 };

 /*这道题采用的是广度优先搜索的算法,对着二叉树一层一层的遍历,而对于题目中的要求,需要在先向右
 又接着向左遍历,所以应用了栈,在偶数行时,先将结点压入栈中,再依次输入到vector中
 */
vector<vector<int> > zigzagLevelOrder(TreeNode *root)
{
	vector<vector<int> > last_result;//最终的存放结果的vector
	if(root==NULL)
		return last_result;

	TreeNode* temp_node;
	queue<TreeNode*> temp;
	int row_size=1;//每一层结点的个数
	temp.push(root);
	int deepth=1;//层数

	while(!temp.empty())
	{
		vector<int> temp_relust;
		stack<int> temp_stack;
		while(row_size--)//每一层遍历
		{
			temp_node=temp.front();
			temp.pop();
			if(temp_node->left!=NULL)
				temp.push(temp_node->left);
			if(temp_node->right!=NULL)
				temp.push((temp_node->right));
			if(deepth%2==0)//偶数层的压栈
			{
				temp_stack.push(temp_node->val);
			}
			else
				temp_relust.push_back(temp_node->val);
		}
		if(deepth%2==0)//偶数层的出栈
		{
			while(!temp_stack.empty())
			{
			  temp_relust.push_back(temp_stack.top());
			  temp_stack.pop();
			}
		}
		row_size=temp.size();
		last_result.push_back(temp_relust);
		deepth++;
	}
	return last_result;
 }
int main()
{

}

  

时间: 2024-12-11 13:44:45

leetcode_103题——Binary Tree Zigzag Level Order Traversal(广度优先搜索,队列queue,栈stack)的相关文章

leetcode 刷题之路 63 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

Binary Tree Zigzag Level Order Traversal

原题: 题目解析:这个问题的实质是要我们按成访问二叉树的结点,并返回每层访问的结果,这里要求走Z字,其实就是一行正向一行反向. /* the kernel idea is visit a binary search tree in level and the additional work we have to label the end of one level. */ vector<vector<int> > zigzagLevelOrder(TreeNode *root) {

[Lintcode]Binary Tree Zigzag Level Order Traversal

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). Example Given binary tree {3,9,20,#,#,1

[leetcode]Binary Tree Zigzag Level Order Traversal @ Python

原题地址:http://oj.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

Binary Tree ZigZag Level Order Traversal leetcode java

题目: 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

39.2: Binary Tree Zigzag Level Order Traversal

/************************************************************************/        /*       38:      Binary Tree Zigzag Level Order Traversal                                       */        /************************************************************

[leetcode]Binary Tree Zigzag Level Order Traversal

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,#

leetcode -day24 Maximum Depth of Binary Tree &amp; Binary Tree Zigzag Level Order Traversal

1.Maximum Depth of Binary Tree Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. class Solution { public: int maxDepth(TreeNode *root) { inM

[LeetCode] Binary Tree Zigzag Level Order Traversal(bfs)

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