# Binary Tree Level Order Traversal

> Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level).

> For example:
Given binary tree {3,9,20,#,#,15,7},

```
3
/ \
9 20
/ \
15 7
```
> return its level order traversal as:

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

1. 对于一棵树，如果我们要求每一层的节点，并且存在一个二维数组里，首先我们要建一个二维数组，但是这个二维数组建多大的合适呢？我们就要求出这颗树的深度，根据深度来建立二维数组.
2. 题目要求为从左往右添加,所以我们也就是要先放左边的节点，再放右边的节点.
3. 对于这道题，我们首先就是要用DFS来求出这颗树的高度，之后再用DFS对于每一层遍历，这样节省了空间复杂度.

```c++
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
/* for this question, we need to construct the ret vector first
thus, we need to know the depth of this tree, we write a simple
function to calculate the height of this tree */
vector > levelOrder(TreeNode *root) {
int depth = getHeight(root);
vector> ret(depth);
if(depth == 0) //invalid check
return ret;
getSolution(ret,root,0);
return ret;
}

void getSolution(vector>& ret, TreeNode* root, int level)
{
if(root == NULL)
return;
ret[level].push_back(root->val);
getSolution(ret,root->left,level+1);
getSolution(ret,root->right,level+1);
}

int getHeight(TreeNode* root)
{
if(root == NULL)
return 0;
int left = getHeight(root->left);
int right = getHeight(root->right);
int height = (left > right?left:right)+1;
return height;
}
};
```

# Binary Tree Level Order Traversal II

> Given a binary tree, return the bottom-up level order traversal of its nodes' values. (from left to right, level by level from leaf to root)

> For example:
Given binary tree {3,9,20,#,#,15,7},

```
3
/ \
9 20
/ \
15 7
```
> return its level order traversal as:

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

```

O(n)-树的dfs均为O(n)

```c++

/**
* 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 > levelOrderBottom(TreeNode *root) {
int depth = height(root);
vector> ret(depth);
if(depth == 0)
return ret;
DFS(ret,ret.size()-1, root);
return ret;
}

void DFS(vector>& ret, int level, TreeNode* root)
{
if(root == NULL)
return;
ret[level].push_back(root->val);
DFS(ret,level-1,root->left);
DFS(ret,level-1,root->right);
}

/* get the height first of all */
int height(TreeNode* root)
{
if(root == NULL)
return 0;
int left_side = height(root->left);
int right_side = height(root->right);
int height = (left_side > right_side?left_side:right_side)+1;
return height;
}
};

```

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

```c++
class Solution {
public:
vector > vals;
vector > zigzagLevelOrder(TreeNode *root) {
build(root, 1);

//翻转
for(int i = 1; i < vals.size(); i+=2) {
reverse(vals[i].begin(), vals[i].end());
}

return vals;
}

void build(TreeNode* node, int level) {
if(!node) {
return;
}

if(vals.size() <= level - 1) {
vals.push_back(vector());
}

vals[level - 1].push_back(node->val);

if(node->left) {
build(node->left, level + 1);
}

if(node->right) {
build(node->right, level + 1);
}
}
};
```