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

	vector<vector<int> > re;
	if(root == NULL)
		return re;

	queue<TreeNode*> visit;
	vector<int> level;

	visit.push(root);
	//NULL stands for the end of this level
	visit.push(NULL);
	//in store every level result, we are asked to change direction.
	bool reback = false;
	while (!visit.empty())
	{
		TreeNode* cur = visit.front();

		visit.pop();
		if(cur != NULL){
			if(cur->left)
				visit.push(cur->left);
			if(cur->right)
				visit.push(cur->right);
			level.push_back(cur->val);
		}

		else
		{
			if(reback)
			{
				vector<int> rlevel(level.rbegin(),level.rend());
				re.push_back(rlevel);

			}
			else
			re.push_back(level);

			//remember to clear this level.
			level.clear();
			reback = !reback;

			if(!visit.empty())
				visit.push(NULL);
		}

	}
	return re;
}

Binary Tree Zigzag Level Order Traversal,布布扣,bubuko.com

时间: 2024-12-16 18:03:29

Binary Tree Zigzag Level Order Traversal的相关文章

39.2: Binary Tree Zigzag Level Order Traversal

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

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

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

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

[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

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 fo

37. Binary Tree Zigzag Level Order Traversal &amp;&amp; Binary Tree Inorder 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,