分类:二叉树
@二叉树前序遍历(递归)
void preOrder1(BinTree *root) //递归前序遍历
{
if(root!=NULL)
{
cout<<root->data<<" ";
preOrder1(root->lchild);
preOrder1(root->rchild);
}
}
@二叉树前序遍历(非递归)
void preOrder2(BinTree *root) //非递归前序遍历
{
stack<BinTree*> s;
BinTree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
cout<<p->data<<" ";
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
s.pop();
p=p->rchild;
}
}
}
@二叉树中序遍历(递归)
void inOrder1(BinTree *root) //递归中序遍历
{
if(root!=NULL)
{
inOrder1(root->lchild);
cout<<root->data<<" ";
inOrder1(root->rchild);
}
}
@二叉树中序遍历(非递归)
void inOrder2(BinTree *root) //非递归中序遍历
{
stack<BinTree*> s;
BinTree *p=root;
while(p!=NULL||!s.empty())
{
while(p!=NULL)
{
s.push(p);
p=p->lchild;
}
if(!s.empty())
{
p=s.top();
cout<<p->data<<" ";
s.pop();
p=p->rchild;
}
}
}
@二叉树后序遍历(递归)
void postOrder1(BinTree *root) //递归后序遍历
{
if(root!=NULL)
{
postOrder1(root->lchild);
postOrder1(root->rchild);
cout<<root->data<<" ";
}
}
@二叉树后序遍历(非递归)
void postOrder2(BinTree *root) //非递归后序遍历
{
stack<BTNode*> s;
BinTree *p=root;
BTNode *temp;
while(p!=NULL||!s.empty())
{
while(p!=NULL) //沿左子树一直往下搜索,直至出现没有左子树的结点
{
BTNode *btn=(BTNode *)malloc(sizeof(BTNode));
btn->btnode=p;
btn->isFirst=true;
s.push(btn);
p=p->lchild;
}
if(!s.empty())
{
temp=s.top();
s.pop();
if(temp->isFirst==true) //表示是第一次出现在栈顶
{
temp->isFirst=false;
s.push(temp);
p=temp->btnode->rchild;
}
else //第二次出现在栈顶
{
cout<<temp->btnode->data<<" ";
p=NULL;
}
}
}
}
@二叉树层序遍历
/**
* 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) {
vector<vector<int>> res;
if(root == NULL)
return res;
vector<int> cur;
std::vector<TreeNode*> v1, v2;
v1.push_back(root);
while(!v1.empty()) {
for(auto x : v1) {
cur.push_back(x->val);
if(x->left) v2.push_back(x->left);
if(x->right) v2.push_back(x->right);
}
res.push_back(cur);
cur.clear();
v2.swap(v1);
v2.clear();
}
return res;
}
};
@已知前序中序(或前序后续,或中序后序)重建二叉树
TreeNode* rebuild(std::vector<int> vec1, std::vector<int> vec2);
@二叉树镜像
TreeNode* mirror(TreeNode* 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 maxDepth(TreeNode* root) {
int d = 0;
if(root != NULL) {
d++;
int d1 = maxDepth(root->left);
int d2 = maxDepth(root->right);
d += max(d1, d2);
}
return d;
}
};
@二叉树中和为某一数值的路径
std::vector<std::vector<int>> findPath(TreeNode* root, int num);
@中序(或前序,或后序)二叉树的下一个节点
TreeNode* findNext(TreeNode* root)
@判断是否为对称二叉树
/**
* 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) {
return isFunc(root, root);
}
bool isFunc(TreeNode* root1, TreeNode* root2) {
if((root1 == NULL && root2 == NULL)
|| (root1 && root2 && root1->val == root2->val
&& isFunc(root1->left, root2->right) && isFunc(root1->right, root2->left))) {
return true;
} else {
return false;
}
}
};
@判断是否为平衡二叉树
/**
* 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 isBalanced(TreeNode* root) {
if(!root) {
return true;
}
int a = hight(root->left);
int b = hight(root->right);
return abs(a - b) <= 1 && isBalanced(root->left) && isBalanced(root->right);
}
int hight(TreeNode* root) {
if(!root) {
return 0;
}
return 1 + max(hight(root->left), hight(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:
bool isValidBST(TreeNode* root) {
if(root == NULL) {
return true;
} else {
bool blcal = true;
bool brcal = true;
bool blBST = true;
bool brBST = true;
if(root->left) {
int imaxLeft = maxVal(root->left);
blcal = imaxLeft < root->val;
blBST = isValidBST(root->left);
}
if(root->right) {
int iminRight = minVal(root->right);
brcal = iminRight > root->val;
brBST = isValidBST(root->right);
}
return blcal && blBST && brcal && brBST;
}
return false;
}
int maxVal(TreeNode* root) {
std::queue<TreeNode*> queNum;
queNum.push(root);
int imax = root->val;
while(!queNum.empty()) {
TreeNode* q = queNum.front();
queNum.pop();
imax = max(imax, q->val);
if(q->left) {
queNum.push(q->left);
}
if(q->right) {
queNum.push(q->right);
}
}
return imax;
}
int minVal(TreeNode* root) {
std::queue<TreeNode*> queNum;
queNum.push(root);
int imin = root->val;
while(!queNum.empty()) {
TreeNode* q = queNum.front();
queNum.pop();
imin = min(imin, q->val);
if(q->left) {
queNum.push(q->left);
}
if(q->right) {
queNum.push(q->right);
}
}
return imin;
}
};
@判断是否相同的树
/**
* 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 isSameTree(TreeNode* p, TreeNode* q) {
if((p == NULL && q == NULL)
|| (p && q && p->val == q->val && isSameTree(p->left, q->left) && isSameTree(p->right, q->right))) {
return true;
}else {
return false;
}
}
};