从上往下打印二叉树
题目描述
从上往下打印出二叉树的每个节点,同层节点从左至右打印。
思路:
层序遍历,用到队列
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int> res;
queue<TreeNode*> que;
if(root==NULL) return res;
que.push(root);
while(!que.empty())
{
TreeNode* tmp=que.front();
que.pop();
res.push_back(tmp->val);
if(tmp->left!=NULL)
que.push(tmp->left);
if(tmp->right!=NULL)
que.push(tmp->right);
}
return res;
}
};
JAVA实现
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public int[] levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
if (root == null) {
return new int[0];
} else {
queue.add(root);
}
Queue<Integer> resQueue = new LinkedList<>();
while (!queue.isEmpty()) {
TreeNode node = queue.peek();
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
resQueue.add(node.val);
queue.poll();
}
int[] res = new int[resQueue.size()];
for (int i = 0; i < res.length; i++) {
res[i] = resQueue.poll();
}
return res;
}
}
把二叉树打印成多行
题目描述:
从上到下按层打印二叉树,同一层结点从左至右输出。每一层输出一行。
思路:
层序遍历,然后用一个变量记录下一层结点数,再用一个变量记录已经存储的这一层的点数。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > res;
queue<TreeNode*> que;
if(!pRoot) return res;
que.push(pRoot);
int levelnum = 0;
int number = 1;
vector<int> tmpv;
while (!que.empty())
{
TreeNode* tmp = que.front();
que.pop();
tmpv.push_back(tmp->val);
if (tmp->left != NULL){
que.push(tmp->left);
levelnum++;
}
if (tmp->right != NULL){
que.push(tmp->right);
levelnum++;
}
--number;
if (number == 0)
{
res.push_back(tmpv);
number = levelnum;
levelnum = 0;
tmpv.clear();
}
}
return res;
}
};
链接:https://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288
来源:牛客网
层次遍历
主要是每一次遍历完一层,可以在大while中新增一个while循环,这个的条件就是队列中剩余的元素个数,这个元素个数在遍历的过程中就是下一层元素的个数啦。
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > vec;
if(pRoot == NULL) return vec;
queue<TreeNode*> q;
q.push(pRoot);
while(!q.empty())
{
int lo = 0, hi = q.size();
vector<int> c;
while(lo++ < hi)
{
TreeNode *t = q.front();
q.pop();
c.push_back(t->val);
if(t->left) q.push(t->left);
if(t->right) q.push(t->right);
}
vec.push_back(c);
}
return vec;
}
};
JAVA解法
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode(int x) { val = x; }
* }
*/
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return new ArrayList<>();
} else {
queue.add(root);
}
while (!queue.isEmpty()) {
List<Integer> tmp = new ArrayList<>();
for (int i = queue.size(); i > 0; i--) {
TreeNode node = queue.poll();
tmp.add(node.val);
if (node.left != null) {
queue.add(node.left);
}
if (node.right != null) {
queue.add(node.right);
}
}
res.add(tmp);
}
return res;
}
}
思路:
用两个队列来存储层序遍历的点
链接:https://www.nowcoder.com/questionTerminal/445c44d982d04483b04a54f298796288
来源:牛客网
//两个队列交错打印
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int>> ret;
if(pRoot==NULL) return ret;
queue<TreeNode*> q1;
queue<TreeNode*> q2;
q1.push(pRoot);
while(!q1.empty()||!q2.empty()){
vector<int> v1;
vector<int> v2;
while(!q1.empty()){
v1.push_back(q1.front()->val);
if(q1.front()->left!=NULL) q2.push(q1.front()->left);
if(q1.front()->right!=NULL) q2.push(q1.front()->right);
q1.pop();
}
if(v1.size()!=0)
ret.push_back(v1);
while(!q2.empty()){
v2.push_back(q2.front()->val);
if(q2.front()->left!=NULL) q1.push(q2.front()->left);
if(q2.front()->right!=NULL) q1.push(q2.front()->right);
q2.pop();
}
if(v2.size()!=0)
ret.push_back(v2);
}
return ret;
}
};
按之字形顺序打印二叉树
题目描述
请实现一个函数按照之字形打印二叉树,即第一行按照从左到右的顺序打印,第二层按照从右至左的顺序打印,第三行按照从左到右的顺序打印,其他行以此类推。
思路:
用两个栈来存储层序遍历的点
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};
*/
class Solution {
public:
vector<vector<int> > Print(TreeNode* pRoot) {
vector<vector<int> > res;
if(pRoot==NULL) return res;
stack<TreeNode*> sta1,sta2;
sta1.push(pRoot);
while(!sta1.empty()||!sta2.empty())//这里必须是或的关系,因为跳出的条件是两个sta都为空
{
vector<int> v1;
vector<int> v2;
while(!sta1.empty())
{
TreeNode* tmp=sta1.top();
sta1.pop();
v1.push_back(tmp->val);
if(tmp->left!=NULL)
sta2.push(tmp->left);
if(tmp->right!=NULL)
sta2.push(tmp->right);
}
if(v1.size())//这里也要有,不然可能会输出空的最后一行
res.push_back(v1);
while(!sta2.empty())
{
TreeNode* tmp=sta2.top();
sta2.pop();
v2.push_back(tmp->val);
if(tmp->right!=NULL)
sta1.push(tmp->right);
if(tmp->left!=NULL)
sta1.push(tmp->left);
}
if(v2.size())//这里也要有判断,不然可能会输出空的最后一行
res.push_back(v2);
}
return res;
}
};
JAVA解法
class Solution {
public static List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue1 = new LinkedList<>();
Queue<TreeNode> queue2 = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if (root == null) {
return new ArrayList<>();
} else {
queue1.add(root);
}
while (!queue1.isEmpty() || !queue2.isEmpty()) {
List<Integer> tmp1 = new ArrayList<>();
for (int i = queue1.size(); i > 0; i--) {
TreeNode node = queue1.poll();
tmp1.add(node.val);
if (node.left != null) {
queue2.add(node.left);
}
if (node.right != null) {
queue2.add(node.right);
}
}
if (tmp1.size() != 0) {
res.add(tmp1);
}
List<Integer> tmp2 = new ArrayList<>();
for (int i = queue2.size(); i > 0; i--) {
TreeNode node = queue2.poll();
tmp2.add(node.val);
if (node.left != null) {
queue1.add(node.left);
}
if (node.right != null) {
queue1.add(node.right);
}
}
if (tmp2.size() != 0) {
Collections.reverse(tmp2);
res.add(tmp2);
}
}
return res;
}
}
并不好,其实应该用queue
层序遍历+倒序
class Solution {
public List<List<Integer>> levelOrder(TreeNode root) {
Queue<TreeNode> queue = new LinkedList<>();
List<List<Integer>> res = new ArrayList<>();
if(root != null) queue.add(root);
while(!queue.isEmpty()) {
List<Integer> tmp = new ArrayList<>();
for(int i = queue.size(); i > 0; i--) {
TreeNode node = queue.poll();
tmp.add(node.val);
if(node.left != null) queue.add(node.left);
if(node.right != null) queue.add(node.right);
}
if(res.size() % 2 == 1) Collections.reverse(tmp);
res.add(tmp);
}
return res;
}
}