1、判断一棵二叉树树是否为另一棵二叉树的子树
class Solution
{
bool IsSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if (pRoot2 == NULL)//第二棵树遍历完,但是第一棵树没有遍历完返回真的
return true;
if (pRoot1 == NULL)//第一棵树遍历完,但是第二棵树没有遍历完返回假的
return false;
if (pRoot2->val == pRoot1->val)
{
return IsSubtree(pRoot1->left, pRoot2->left)
&& IsSubtree(pRoot1->right, pRoot2->right);
}
else return false;
}
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
if (pRoot1 == NULL || pRoot2 == NULL)
return false;
return IsSubtree(pRoot1, pRoot2)
|| HasSubtree(pRoot1->left, pRoot2)
|| HasSubtree(pRoot1->right, pRoot2);
}
};
2、二叉树的镜像
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
void Mirror(TreeNode *pRoot)
{
if(pRoot==NULL)
return;
if(pRoot->left==NULL&&pRoot->right==NULL)
return ;
TreeNode *tmp=pRoot->left;
pRoot->left=pRoot->right;
pRoot->right=tmp;
if(pRoot->left!=NULL)
Mirror(pRoot->left);
if(pRoot->right!=NULL)
Mirror(pRoot->right);
}
};
3、打印出二叉树的每个节点
思路:层序遍历
代码:
class Solution {
public:
//二叉树的层序遍历
vector<int> PrintFromTopToBottom(TreeNode* root)
{
vector<int> v;
queue<TreeNode*> q;
if(root==NULL)
return v;
q.push(root);
while(!q.empty())
{
TreeNode* ret=q.front();
q.pop();
if(ret)
{
v.push_back(ret->val);
q.push(ret->left);
q.push(ret->right);
}
}
return v;
}
};
4、二叉树中和为某一值的路径
注意:一定要到叶子结点,只是路径的一部分是不可以的
代码:
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution
{
vector<int> v;
vector<vector<int>> res;
void Find(TreeNode* root,int expectNumber)
{
if(root==NULL)
return;
v.push_back(root->val);
if(!root->left&&!root->right&&root->val==expectNumber)
res.push_back(v);
else
{
if(root->left)
Find(root->left,expectNumber-root->val);
if(root->right)
Find(root->right,expectNumber-root->val);
}
v.pop_back();
}
public:
vector<vector<int> > FindPath(TreeNode* root,int expectNumber)
{
Find(root,expectNumber);
return res;
}
};
5、二叉树与双向链表
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution
{
TreeNode* head=nullptr;
TreeNode* realhead=nullptr;
void ConvertHelp(TreeNode* pRootOfTree)
{
if(pRootOfTree==NULL)
return;
ConvertHelp(pRootOfTree->left);
if(head==nullptr)
{
head=pRootOfTree;
realhead=pRootOfTree;
}
else
{
//变成一个双向链表
head->right=pRootOfTree;
pRootOfTree->left=head;
head=pRootOfTree;
}
ConvertHelp(pRootOfTree->right);
}
public:
TreeNode* Convert(TreeNode* pRootOfTree)
{
if(pRootOfTree==NULL)
return NULL;
ConvertHelp(pRootOfTree);
return realhead;
}
};
6、顺时针打印矩阵(此题和二叉树无关)
class Solution {
public:
vector<int> printMatrix(vector<vector<int> > matrix)
{
vector<int> temp;
int col = matrix[0].size(); //列
int row = matrix.size(); //行
if (row == 0 || col == 0)
return temp;
int top = 0;
int right = col - 1;
int left = 0;
int bottom = row - 1;
while (top <= bottom && left <= right)
{
//右
for (int i = left; i <= right; i++)
{
temp.push_back(matrix[top][i]);
}
//下
for (int i = top + 1; i <= bottom; i++)
{
temp.push_back(matrix[i][right]);
}
//左
for (int i = right - 1; i >= left && top < bottom; i--)
{
temp.push_back(matrix[bottom][i]);
}
//上
for (int i = bottom - 1; i > top&&right > left; i--)
{
temp.push_back(matrix[i][top]);
}
bottom--; right--; left++; top++;
}
return temp;
}
};