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

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its level order traversal as:

  1. [
  2. [3],
  3. [9,20],
  4. [15,7]
  5. ]

题目翻译:
给定一颗二叉树,返回一个二维数组,使这个二维数组的每一个元素代表着二叉树的一层的元素.例子已经明确给出.

题目分析:
对于二叉树的问题,我们第一想到的就是DFS或者BFS, DFS更易于理解代码,如果处理数据量不是很大的话.对于这样的面试题,我建议用DFS来求解.

需要注意的点为:

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

时间复杂度分析:
由于两次DFS是并列的,并没有嵌套,所以我们的时间复杂度为O(n).

代码如下:

  1. /**
  2. * Definition for binary tree
  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. /* for this question, we need to construct the ret vector first
  13. thus, we need to know the depth of this tree, we write a simple
  14. function to calculate the height of this tree */
  15. vector<vector<int> > levelOrder(TreeNode *root) {
  16. int depth = getHeight(root);
  17. vector<vector<int>> ret(depth);
  18. if(depth == 0) //invalid check
  19. return ret;
  20. getSolution(ret,root,0);
  21. return ret;
  22. }
  23. void getSolution(vector<vector<int>>& ret, TreeNode* root, int level)
  24. {
  25. if(root == NULL)
  26. return;
  27. ret[level].push_back(root->val);
  28. getSolution(ret,root->left,level+1);
  29. getSolution(ret,root->right,level+1);
  30. }
  31. int getHeight(TreeNode* root)
  32. {
  33. if(root == NULL)
  34. return 0;
  35. int left = getHeight(root->left);
  36. int right = getHeight(root->right);
  37. int height = (left > right?left:right)+1;
  38. return height;
  39. }
  40. };

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

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its level order traversal as:

  1. [
  2. [15,7],
  3. [9,20],
  4. [3]
  5. ]

题目翻译:
给定一颗二叉树, 返回一个二维数组,这个二维数组要满足这个条件,二维数组的第一个一维数组要是这可二叉树的最下面一层,之后以此类推,根据以上例子应该知道要求的条件。

题目分析 && 解题思路:
这道题和Binary Tree Level Order Traversal 几乎是一摸一样的,唯一不同的就是二维数组的存储顺序,详见以下代码.

时间复杂度:
O(n)-树的dfs均为O(n)

代码如下:

  1. /**
  2. * Definition for binary tree
  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> > levelOrderBottom(TreeNode *root) {
  13. int depth = height(root);
  14. vector<vector<int>> ret(depth);
  15. if(depth == 0)
  16. return ret;
  17. DFS(ret,ret.size()-1, root);
  18. return ret;
  19. }
  20. void DFS(vector<vector<int>>& ret, int level, TreeNode* root)
  21. {
  22. if(root == NULL)
  23. return;
  24. ret[level].push_back(root->val);
  25. DFS(ret,level-1,root->left);
  26. DFS(ret,level-1,root->right);
  27. }
  28. /* get the height first of all */
  29. int height(TreeNode* root)
  30. {
  31. if(root == NULL)
  32. return 0;
  33. int left_side = height(root->left);
  34. int right_side = height(root->right);
  35. int height = (left_side > right_side?left_side:right_side)+1;
  36. return height;
  37. }
  38. };

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

  1. 3
  2. / \
  3. 9 20
  4. / \
  5. 15 7

return its zigzag level order traversal as:

  1. [
  2. [3],
  3. [20,9],
  4. [15,7]
  5. ]

如果完成了上面两题,这题应该是很简单的,我们只需要将得到的数据按照zigzag的方式翻转一下,代码如下:

  1. class Solution {
  2. public:
  3. vector<vector<int> > vals;
  4. vector<vector<int> > zigzagLevelOrder(TreeNode *root) {
  5. build(root, 1);
  6. //翻转
  7. for(int i = 1; i < vals.size(); i+=2) {
  8. reverse(vals[i].begin(), vals[i].end());
  9. }
  10. return vals;
  11. }
  12. void build(TreeNode* node, int level) {
  13. if(!node) {
  14. return;
  15. }
  16. if(vals.size() <= level - 1) {
  17. vals.push_back(vector<int>());
  18. }
  19. vals[level - 1].push_back(node->val);
  20. if(node->left) {
  21. build(node->left, level + 1);
  22. }
  23. if(node->right) {
  24. build(node->right, level + 1);
  25. }
  26. }
  27. };