Boostable

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

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

地址:https://oj.leetcode.com/problems/binary-tree-level-order-traversal/
算法:BFS一颗树,然后分别输出每一层的节点值。在BFS遍历过程中,标识出每一层的结束节点即可。第一层的结束节点即为根,根据第一层的结束节点就可以得到第二层的结束节点,依次类推就可以得到各层的结束节点。代码:
 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> > levelOrder(TreeNode *root) {
13         if(!root)   return vector<vector<int> >();
14         queue<TreeNode *> que;
15         que.push(root);
16         int push_num = 1;
17         int pop_num  = 0;
18         vector<vector<int> > result;
19         vector<int> temp;
20         int last  = 1;
21         while(!que.empty()){
22             TreeNode *pop_node = que.front();
23             temp.push_back(pop_node->val);
24             que.pop();
25             ++pop_num;
26             if(pop_node->left){
27                 que.push(pop_node->left);
28                 ++push_num;
29             }
30             if(pop_node->right){
31                 que.push(pop_node->right);
32                 ++push_num;
33             }
34             if(pop_num == last){
35                 result.push_back(temp);
36                 temp.clear();
37                 last = push_num;
38             }
39         }
40         return result;
41     }
42 };

第二题:

Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, 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 bottom-up level order traversal as:

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

地址:https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/
算法:这一题要求先输出最后一层的节点。在上一题的基础上在吧结果转置一下,这样的方法会不会很不高端啊?代码:
 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         if(!root)   return vector<vector<int> >();
14         queue<TreeNode *> que;
15         que.push(root);
16         int push_num = 1;
17         int pop_num  = 0;
18         vector<vector<int> > result;
19         vector<int> temp;
20         int last  = 1;
21         while(!que.empty()){
22             TreeNode *pop_node = que.front();
23             temp.push_back(pop_node->val);
24             que.pop();
25             ++pop_num;
26             if(pop_node->left){
27                 que.push(pop_node->left);
28                 ++push_num;
29             }
30             if(pop_node->right){
31                 que.push(pop_node->right);
32                 ++push_num;
33             }
34             if(pop_num == last){
35                 result.push_back(temp);
36                 temp.clear();
37                 last = push_num;
38             }
39         }
40         temp.clear();
41         int len = result.size();
42         int half_len = len / 2;
43         for(int i = 0; i < half_len; ++i){
44             temp = result[i];
45             result[i] = result[len - i - 1];
46             result[len - i - 1] = temp;
47         }
48         return result;
49     }
50 };

 

posted on 2014-08-27 21:28  Boostable  阅读(381)  评论(0编辑  收藏  举报