码迷,mamicode.com
首页 > 其他好文 > 详细

Leetcode 102 Binary Tree Level Order Traversal

时间:2015-07-03 06:55:11      阅读:121      评论:0      收藏:0      [点我收藏+]

标签:

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. 用一个 current point来指示当前层,然后每次压入的时候不断更新,用一个tail来标记父节点的last pointer,当父节点走到最后了,把current赋给tail
 1 class Solution {
 2 public:
 3     vector<vector<int> > levelOrder(TreeNode* root) {
 4         vector<vector<int> > v;
 5         vector<int> level;
 6         if (root == NULL)
 7             return v; 
 8         if (root ->left == NULL && root ->right == NULL)
 9         {
10             level.push_back(root->val);
11             v.push_back(level);
12             return v; 
13         }
14         queue<TreeNode*> q;
15         q.push(root);
16         TreeNode *trail,*cur; // trail record the last item in each level
17         trail = cur = root; 
18         
19         while(!q.empty())
20         {
21             TreeNode* pointer = q.front();
22             q.pop();
23             level.push_back(pointer->val);
24             if (pointer->left != NULL)
25             {
26                 cur = pointer->left;
27                 q.push(pointer->left);
28             }
29             if (pointer->right != NULL)
30             {
31                 cur = pointer->right;
32                 q.push(pointer->right);
33             }
34             if (pointer == trail)
35             {
36                 trail = cur;
37                 v.push_back(level);
38                 level.clear();
39             }    
40         }
41         return v;       
42     }
43 };

2)第二个方法是,用NULL做一个,把NULL也压入queue里面,到访问到NULL的时候,就意味着是这层的最后一个 

 1 class Solution {
 2 public:
 3     vector<vector<int> > levelOrder(TreeNode *root) {
 4         vector<vector<int> >  result;
 5         if (!root) return result;
 6         queue<TreeNode*> q;
 7         q.push(root);
 8         q.push(NULL);
 9         vector<int> cur_vec;
10         while(!q.empty()) {
11             TreeNode* t = q.front();
12             q.pop();
13             if (t==NULL) {
14                 result.push_back(cur_vec);
15                 cur_vec.resize(0);
16                 if (q.size() > 0) {
17                     q.push(NULL);
18                 }
19             } else {
20                 cur_vec.push_back(t->val);
21                 if (t->left) q.push(t->left);
22                 if (t->right) q.push(t->right);
23             }
24         }
25         return result;
26     }
27 };

3) preorder 用递归做,判断何时新加一个vector

 1 vector<vector<int>> ret;
 2 
 3 void buildVector(TreeNode *root, int depth)
 4 {
 5     if(root == NULL) return;
 6     if(ret.size() == depth)
 7         ret.push_back(vector<int>());
 8 
 9     ret[depth].push_back(root->val);
10     buildVector(root->left, depth + 1);
11     buildVector(root->right, depth + 1);
12 }
13 
14 vector<vector<int> > levelOrder(TreeNode *root) {
15     buildVector(root, 0);
16     return ret;
17 }

4) 可以在压入队列的时候同时压入层次的数字

 

Leetcode 102 Binary Tree Level Order Traversal

标签:

原文地址:http://www.cnblogs.com/zhuguanyu33/p/4617590.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!