LeetCode Binary Tree Level Order Traversal
2014-11-05 19:16
155 查看
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
return its level order traversal as:
confused what
read more on how binary tree is serialized on OJ.
题意:给出一个二叉树,求其的层次遍历
思路:用BFS方法,在每一层后加null,从队列中取出一个,如果不为null,就认为是同一层,直接加到该层的最后,如果为null,说明是一层的结束,此时队列为空,就退出,不为空,将null也入队列,再新建一层
另一种思路:用DFS方法,分别求左、右子树的层次遍历结果,再合并
第三种解:用BFS,先将根结点入队列,在队列操作中,取出一个结点,如果非空,将左、右树入队列(不管是不是空),同时将入队列的结点放入一个数组中。接着是对这个数组操作,在扫描当前层时,可以计算下一层的结束下标,以此类推
/**
* 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<vector<int> > levelOrder(TreeNode *root) {
queue<TreeNode*> q;
vector<vector<int> > vv;
vector<int> v;
if (root) {
v.push_back(root->val);
vv.push_back(v);
}
q.push(root);
vector<TreeNode*> vt;
while (!q.empty()) {
TreeNode* cur = q.front(); q.pop();
vt.push_back(cur);
if (cur == NULL) continue;
q.push(cur->left);
q.push(cur->right);
}
int step = 2;
size_t j;
for (size_t i = 1; i < vt.size(); i = j) {
v.clear();
int cnt = 0;
for (j = i; j < i + step && j < vt.size(); j++) {
if (vt[j]) {
v.push_back(vt[j]->val);
cnt += 2;
}
}
step = cnt;
if (v.size() > 0) {
vv.push_back(v);
}
}
return vv;
}
};
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] ]
confused what
"{1,#,2,3}"means? >
read more on how binary tree is serialized on OJ.
题意:给出一个二叉树,求其的层次遍历
思路:用BFS方法,在每一层后加null,从队列中取出一个,如果不为null,就认为是同一层,直接加到该层的最后,如果为null,说明是一层的结束,此时队列为空,就退出,不为空,将null也入队列,再新建一层
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public List<List<Integer>> levelOrder(TreeNode root) { List<List<Integer>> ans = new ArrayList<List<Integer>>(); ArrayList<Integer> level = null; Queue<TreeNode> q = new LinkedList<TreeNode>(); if (root != null) { q.add(root); q.add(null); } level = new ArrayList<Integer>(); while (!q.isEmpty()) { TreeNode cur = q.poll(); if (cur == null) { ans.add(level); if (q.isEmpty()) break; q.add(null); level = new ArrayList<Integer>(); continue; } level.add(cur.val); if (cur.left != null) { q.add(cur.left); } if (cur.right != null) { q.add(cur.right); } } return ans; } }
另一种思路:用DFS方法,分别求左、右子树的层次遍历结果,再合并
/** * 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<vector<int> > levelOrder(TreeNode *root) { vector<vector<int> > vv; vector<int> v; if (root) { v.push_back(root->val); vv.push_back(v); } vector<vector<int> > vvleft, vvright; if (root && root->left) { vvleft = levelOrder(root->left); } if (root && root->right) { vvright = levelOrder(root->right); } for (int i = 0; i < vvleft.size() || i < vvright.size(); i++) { if (i < vvleft.size() && i < vvright.size()) { vvleft[i].insert(vvleft[i].end(), vvright[i].begin(), vvright[i].end()); vv.push_back(vvleft[i]); } else if (i < vvleft.size()) { vv.push_back(vvleft[i]); } else { vv.push_back(vvright[i]); } } return vv; } };
第三种解:用BFS,先将根结点入队列,在队列操作中,取出一个结点,如果非空,将左、右树入队列(不管是不是空),同时将入队列的结点放入一个数组中。接着是对这个数组操作,在扫描当前层时,可以计算下一层的结束下标,以此类推
/**
* 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<vector<int> > levelOrder(TreeNode *root) {
queue<TreeNode*> q;
vector<vector<int> > vv;
vector<int> v;
if (root) {
v.push_back(root->val);
vv.push_back(v);
}
q.push(root);
vector<TreeNode*> vt;
while (!q.empty()) {
TreeNode* cur = q.front(); q.pop();
vt.push_back(cur);
if (cur == NULL) continue;
q.push(cur->left);
q.push(cur->right);
}
int step = 2;
size_t j;
for (size_t i = 1; i < vt.size(); i = j) {
v.clear();
int cnt = 0;
for (j = i; j < i + step && j < vt.size(); j++) {
if (vt[j]) {
v.push_back(vt[j]->val);
cnt += 2;
}
}
step = cnt;
if (v.size() > 0) {
vv.push_back(v);
}
}
return vv;
}
};
相关文章推荐
- LeetCode #107 - Binary Tree Level Order Traversal II
- [leetcode] Binary Tree Level Order Traversal
- [LeetCode] Binary Tree Level Order Traversal II
- 【LeetCode】Binary Tree Level Order Traversal II
- LeetCode-Binary Tree Level Order Traversal II
- Binary Tree Level Order Traversal - LeetCode
- [leetcode]Binary Tree Level Order Traversal II
- [leetcode]Binary Tree Zigzag Level Order Traversal
- Leetcode-Binary Tree Level Order Traversal
- Leetcode 树 Binary Tree Level Order Traversal
- leetcode[103]Binary Tree Zigzag Level Order Traversal
- leetcode---binary-tree-level-order-traversal-ii---树层次遍历
- Leetcode-107(Java) Binary Tree Level Order Traversal II
- leetcode - Binary Tree Level Order Traversal II
- LeetCode | Binary Tree Level Order Traversal
- leetcode-102 Binary Tree Level Order Traversal
- LeetCode(103) Binary Tree Zigzag Level Order Traversal
- [leetcode: Python]107.Binary Tree Level Order Traversal 2
- 【LeetCode】-Binary Tree Level Order Traversal II
- Leetcode: Binary Tree Level Order Traversal: Two Queue