递归三部曲:
1.确定递归函数的参数和返回值
2.确定终止条件
3.确定单层递归的逻辑
递归:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traversal(TreeNode* cur,vector<int>& res){
if(cur==nullptr) return;
res.push_back(cur->val);
traversal(cur->left,res);
traversal(cur->right,res);
}
vector<int> preorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root,res);
return res;
}
};
迭代法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> preorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> res;
if(root==nullptr) return res;
st.push(root);
while(!st.empty()){
TreeNode* cur=st.top();
res.push_back(cur->val);
st.pop();
if(cur->right) st.push(cur->right);
if(cur->left) st.push(cur->left);
}
return res;
}
};
递归法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traversal(TreeNode* cur,vector<int>& res){
if(cur==nullptr) return;
traversal(cur->left,res);
traversal(cur->right,res);
res.push_back(cur->val);
}
vector<int> postorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root,res);
return res;
}
};
迭代法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> postorderTraversal(TreeNode* root) {
stack<TreeNode*> st;
vector<int> res;
if(root==nullptr) return res;
st.push(root);
while(!st.empty()){
TreeNode* cur=st.top();
res.push_back(cur->val);
st.pop();
if(cur->left) st.push(cur->left);
if(cur->right) st.push(cur->right);
}
reverse(res.begin(),res.end());
return res;
}
};
递归法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
void traversal(TreeNode* cur,vector<int>& res){
if(cur==nullptr) return;
traversal(cur->left,res);
res.push_back(cur->val);
traversal(cur->right,res);
}
vector<int> inorderTraversal(TreeNode* root) {
vector<int> res;
traversal(root,res);
return res;
}
};
迭代法:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<int> inorderTraversal(TreeNode* root) {
vector<int>res;
stack<TreeNode*> st;
TreeNode* cur=root;
while(cur!=nullptr||!st.empty()){
if(cur!=nullptr){
st.push(cur);
cur=cur->left;
}
else{
cur=st.top();
st.pop();
res.push_back(cur->val);
cur=cur->right;
}
}
return res;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
vector<vector<int>> levelOrder(TreeNode* root) {
queue<TreeNode*> que;
vector<vector<int>> res;
if(root==nullptr) return res;
TreeNode* cur=root;
que.push(cur);
while(!que.empty()){
int size=que.size();//确定每层的节点数量
vector<int> vec;
for(int i=0;i<size;i++){//根据上层的节点数量处理上层
cur=que.front();
vec.push_back(cur->val);//将上层节点值压入res,弹出que
que.pop();
if(cur->left) que.push(cur->left);//将弹出que的节点的左右孩子节点压入que;
if(cur->right) que.push(cur->right);
}
res.push_back(vec);
}
return res;
}
};
层序遍历解决
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
TreeNode* invertTree(TreeNode* root) {
queue<TreeNode*> que;
if(root!=nullptr) que.push(root);
while(!que.empty()){
int size=que.size();
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
swap(cur->left,cur->right);
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return root;
}
};
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
bool compare(TreeNode*left,TreeNode*right){//确定递归参数和返回值
//确定终止条件
if(left==nullptr&&right!=nullptr) return false;//左子树为空,右子树不空
else if(left!=nullptr&&right==nullptr) return false;//左子树不空,右子树空
else if(left==nullptr&&right==nullptr) return true;//左右子树都空
else if(left->val!=right->val) return false;//左右子树
//单层循环
bool outside=compare(left->left,right->right);
bool inside=compare(left->right,right->left);
bool issame=outside&&inside;
return issame;
}
bool isSymmetric(TreeNode* root) {
if(root==nullptr) return true;
return compare(root->left,root->right);
}
};
后序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {//确定参数和返回值
//确定终止条件
if(root==nullptr) return 0;
//确定单层循环逻辑
int leftDept=maxDepth(root->left);
int rightDept=maxDepth(root->right);
int dept=max(leftDept,rightDept);
return dept+1;
}
};
前序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res;
void getDept(TreeNode* root,int depth){//确定递归参数和返回值
//确定终止条件
if(root==nullptr) return;
//确定单层循环
res=depth>res?depth:res;
//左孩子
depth++;
getDept(root->left,depth);
depth--;
//右孩子
depth++;
getDept(root->right,depth);
depth--;
return;
}
int maxDepth(TreeNode* root) {
res=1;
if(root==nullptr) return 0;
getDept(root,res);
return res;
}
};
层序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int maxDepth(TreeNode* root) {
if(root==nullptr) return 0;
int depth=0;
queue<TreeNode*> que;
que.push(root);
while(!que.empty()){
int size=que.size();
depth++;
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return depth;
}
};
判断收获的地方的时候,是这个节点的左右子节点都为空的时候,并且注意是叶节点到根节点。如果节点数是1,那深度就是1,但是如果节点不是1,就要注意最短一定不是1。
后序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {//确定递归参数和返回值
//确定终止条件
if(root==nullptr) return 0;
//确定单层递归逻辑
int leftDepth=minDepth(root->left);
int rightDepth=minDepth(root->right);
int depth;
if(root->left&&!root->right) depth=1+leftDepth;
else if(!root->left&&root->right) depth=1+rightDepth;
else depth=min(leftDepth,rightDepth)+1;
return depth;
}
};
前序:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int res;
void getDepth(TreeNode* cur,int depth){//确定函数参数和返回值
//确定终止条件
if(cur==nullptr) return;
//确定单层递归逻辑
if(cur->left==nullptr&&cur->right==nullptr){
res=min(depth,res);
}
getDepth(cur->left,depth+1);
getDepth(cur->right,depth+1);
return;
}
int minDepth(TreeNode* root) {
if(root==nullptr) return 0;
res=INT_MAX;
getDepth(root,1);
return res;
}
};
迭代法:(层序)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int minDepth(TreeNode* root) {
queue<TreeNode*> que;
int depth=0;
if(root!=nullptr) que.push(root);
while(!que.empty()){
int size=que.size();
depth++;
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
if(cur->left==nullptr&&cur->right==nullptr) return depth;
}
}
return depth;
}
};
迭代法:层序遍历
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int countNodes(TreeNode* root) {
queue<TreeNode*> que;
if(root!=nullptr) que.push(root);
int res=0;
while(!que.empty()){
int size=que.size();
res+=size;
for(int i=0;i<size;i++){
TreeNode* cur=que.front();
que.pop();
if(cur->left) que.push(cur->left);
if(cur->right) que.push(cur->right);
}
}
return res;
}
};
递归法:(后序遍历)
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int getNum(TreeNode* cur){//确定递归函数参数和返回值
//确定终止条件
if(cur==nullptr) return 0;
//确定单层递归逻辑
int leftNum=getNum(cur->left);
int rightNum=getNum(cur->right);
int midNum=leftNum+rightNum+1;
return midNum;
}
int countNodes(TreeNode* root) {
return getNum(root);
}
};
利用完全二叉树特性:
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode() : val(0), left(nullptr), right(nullptr) {}
* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
* };
*/
class Solution {
public:
int geNum(TreeNode* cur){//确定递归函数参数和返回值
if(cur==nullptr) return 0;
//确定终止条件
int leftNum=0,rightNum=0;
TreeNode* left=cur->left;
TreeNode* right=cur->right;
while(left){
leftNum++;
left=left->left;
}
while(right){
rightNum++;
right=right->right;
}
if(leftNum==rightNum) return (2<<leftNum)-1;
//单层递归逻辑
int leftTreeNum=geNum(cur->left);
int rightTreeNum=geNum(cur->right);
int midTreeNum=leftTreeNum+rightTreeNum+1;
return midTreeNum;
}
int countNodes(TreeNode* root) {
return geNum(root);
}
};