[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] ]
confused what"{1,#,2,3}"means? > read more on how binary tree is serialized on OJ.
OJ's Binary Tree Serialization:
The serialization of a binary tree follows a level order traversal, where '#' signifies a path terminator where no node exists below.
Here's an example:
1 / \ 2 3 / 4 \ 5The above binary tree is serialized as"{1,2,3,#,#,4,#,#,5}".
二叉树的层次遍历一般是利用队列结构,先将root入队,然后在队列变空之前反复的迭代。迭代部分:首先是取出队首节点并访问,左孩子入队,然后右孩子入队。
方法一:@牛客网NBingGee
因为这题是以每层的形式输出,不是整体。所以需要一个中间变量levelNode来存放每层的节点,关键在于如何层与层之间的节点分开。可以用两个计数器,一个存放当前层的节点个数(levCount),一个存放下一层的节点个数(count)。如果levCount==0,则将当前层的节点存入res中,更新levCount并进入下一行。过程中,二叉树层次遍历的整体思想不变,只不过在循环体的最后加了一段判断是否存入res的代码。
/** * 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>> res; vector<int> levelNode; queue<TreeNode *> Q; if(root) Q.push(root); int count=0; //下一层元素的个数 int levCount=1; //当前层元素个数,初始为第一层 while( !Q.empty()) { TreeNode *cur=Q.front(); levelNode.push_back(cur->val); Q.pop(); levCount--; if(cur->left) { Q.push(cur->left); count++; } if(cur->right) { Q.push(cur->right); count++; } if(levCount==0) { res.push_back(levelNode); levCount=count; count=0; levelNode.clear(); //清空levelNode,为下层 } } return res; } };
方法二:
思路:遍历完一层以后,队列中节点的个数就是二叉树下一层的节点数。实时更新队列中节点的个数,每层的遍历。
/** * 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>> res; queue<TreeNode *> Q; if(root) Q.push(root); while( !Q.empty()) { int count=0; int levCount=Q.size(); vector<int> levNode; //遍历当前层 while(count<levCount) { TreeNode *curNode=Q.front(); Q.pop(); levNode.push_back(curNode->val); if(curNode->left) Q.push(curNode->left); if(curNode->right) Q.push(curNode->right); count++; } res.push_back(levNode); } return res; } };
方法三:
利用队列,在每一层结束时向栈中压入NULL, 则遇到NULL就标志一层的结束,就可以存节点了。
/** * 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>> res; queue<TreeNode *> Q; if(!root) return res; Q.push(root); Q.push(NULL); vector<int> levNode; //存放每层的结点的值 while( !Q.empty()) { TreeNode *cur=Q.front(); Q.pop(); if(cur) { levNode.push_back(cur->val); if(cur->left) Q.push(cur->left); if(cur->right) Q.push(cur->right); } else { res.push_back(levNode); levNode.clear(); if( !Q.empty()) Q.push(NULL); } } return res; } };