102. 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,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its level order traversal as:
[ [3], [9,20], [15,7] ]
Solution: 先序遍历或者后序遍历都行,遍历的同时记录一下深度就可以了
Code(递归版):
/**
* Definition for a binary tree node.
* 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>> ans;
travel(root, 0, ans);
return ans;
}
private:
void travel(TreeNode* root, int depth, vector<vector<int>>& m){
if(root==NULL) return;
if(depth>=m.size()) m.push_back(vector<int>());
travel(root->left, depth+1, m);
travel(root->right, depth+1, m);
m[depth].push_back(root->val);
}
};
Code(迭代版): 使用queue记录每一层。
/**
* Definition for a binary tree node.
* 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>> ans;
queue<TreeNode*> cur,next;
if(root==NULL) return ans;
cur.push(root);
vector<int> levelval;
while(!cur.empty()){
while(!cur.empty()){
if(cur.front()->left!=NULL) next.push(cur.front()->left);
if(cur.front()->right!=NULL) next.push(cur.front()->right);
levelval.push_back(cur.front()->val);
cur.pop();
}
swap(cur,next);
ans.push_back(levelval);
levelval.clear();
}
return ans;
}
};
107. Binary Tree Level Order Traversal II
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,null,null,15,7]
,
3 / \ 9 20 / \ 15 7
return its bottom-up level order traversal as:
[ [15,7], [9,20], [3] ]
Solution: 对前一题的结果进行翻转即可。
Code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrderBottom(TreeNode* root) {
vector<vector<int>> ans;
getSons(root, 0, ans);
reverse(ans.begin(), ans.end()); //翻转
return ans;
}
private:
void getSons(TreeNode* root, int d, vector<vector<int>>& v){
if(root==NULL) return;
if(d>=v.size()) v.push_back(vector<int>());
getSons(root->left, d+1, v);
getSons(root->right, d+1, v);
v[d].push_back(root->val);
}
};
101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center).
For example, this binary tree [1,2,2,3,4,4,3]
is symmetric:
1 / \ 2 2 / \ / \ 3 4 4 3
But the following [1,2,2,null,3,null,3]
is not:
1 / \ 2 2 \ \ 3 3
Note:
Bonus points if you could solve it both recursively and iteratively.
Code:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool isSymmetric(TreeNode* root) {
if(root==NULL) return true;
return isSym(root->left, root->right);
}
private:
bool isSym(TreeNode* l, TreeNode* r){
if(l==NULL && r==NULL) return true;
if(l!=NULL && r!=NULL){
if(l->val != r->val) return false;
return isSym(l->left, r->right) && isSym(l->right, r->left);
}
return false;
}
};