二叉树有几种遍历方式:
前序遍历:根左右
中序遍历:左根右
后序遍历:左右根
(a)
前序遍历:1 2 4 8 5 3 6 7
中序遍历:8 4 2 5 1 6 7 3
后序遍历:8 4 5 2 6 7 3 1
1、二叉树的深度。
输入一颗二叉树的根结点,求该树的深度。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
//根节点既有左子树又有右子树,先获得左子树的深度,再获得右子树的深度,用递归方法实现,根节点的深度就为左右子树最深的深度+1;
//特殊用例为根节点为空
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
if(pRoot==NULL)
return NULL;
int DepthofLeft=TreeDepth(pRoot->left);
int DepthofRight=TreeDepth(pRoot->right);
int treeDepth=(DepthofLeft>DepthofRight)?(DepthofLeft+1):(DepthofRight+1);
return treeDepth;
}
};`
2、二叉树中和为某一值得路径。输入一颗二叉树和一个证书,打印出二叉树中节点值得和为如数整数的所有路径。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<vector<int> > ans;//放路径的
vector<int>path;//找到和为某值得路径
void FindPathl(TreeNode* root,int expectNumber1)
{
if(root==NULL)
return;
path.push_back(root->val);
if(root->left==NULL&&root->right==NULL&&expectNumber1==root->val)
{
ans.push_back(path);
}
else{
if(root->left!=NULL)
{
FindPathl(root->left,expectNumber1-root->val);
}
if(root->right!=NULL)
{
FindPathl(root->right,expectNumber1-root->val);
}
}
path.pop_back();
}
vector<vector<int> > FindPath(TreeNode* root,int expectNumber) {
FindPathl(root,expectNumber);
return ans;
}
};
3、重建二叉树。输入某二叉树的前序遍历和中序遍历的结果,重建出该二叉树。
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
TreeNode* reConstructBinaryTree(vector<int> pre,vector<int> vin) {//pre前序遍历,vin中序遍历
int length=vin.size();
if(length ==0)
return NULL;
int root = pre[0];
vector<int> pre_right,pre_left,vin_right,vin_left;
TreeNode * head = new TreeNode(root);
int i=0;
for(i=0;i<length;i++)
{
if(vin[i]==root)
break;
}
for(int j=0;j<length;j++)
{
if(j<i)//左子树
{
pre_left.push_back(pre[j+1]);//pre[0]是根结点,所以要从pre[j+1]开始
vin_left.push_back(vin[j]);
}
else if(j>i)
{
pre_right.push_back(pre[j]);
vin_right.push_back(vin[j]);
}
}
head->left=reConstructBinaryTree(pre_left,vin_left);
head->right=reConstructBinaryTree(pre_right,vin_right);
return head;
}
};
4、树的子结构
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
bool result=false;
if(pRoot1!=NULL&&pRoot2!=NULL)
{
if(pRoot1->val==pRoot2->val)
{
result=HasSubtree2(pRoot1,pRoot2);
}
if(!result)
{
result= HasSubtree(pRoot1->left,pRoot2);
}
if(!result)
{
result= HasSubtree(pRoot1->right,pRoot2);
}
}
return result;
}
bool HasSubtree2(TreeNode*pRoot1,TreeNode* pRoot2)
{
if(pRoot1==NULL&&pRoot2!=NULL)
return false;
if(pRoot2==NULL)
return true;
if(pRoot1->val!=pRoot2->val)
{
return false;
}
return HasSubtree2(pRoot1->left,pRoot2->left)&&HasSubtree2(pRoot1->right,pRoot2->right);
}
};
5、从上到下遍历二叉树:层序遍历二叉树
从上往下打印二叉树的每个节点,同一层的结点按照从左到右的顺序打印。
/*
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
TreeNode(int x) :
val(x), left(NULL), right(NULL) {
}
};*/
class Solution {
public:
vector<int> PrintFromTopToBottom(TreeNode* root) {
vector<int>res;//存放队列的头结点
queue<TreeNode* > que;//队列先进先出
if(root!=NULL)
que.push(root);
while(que.size())
{
TreeNode* pNode=que.front();
res.push_back(pNode->val);
if(pNode->left!=NULL)
{
que.push(pNode->left);
}
if(pNode->right!=NULL)
{
que.push(pNode->right);
}
que.pop();
}
return res;
}
};
6、二叉搜索树后序遍历序列
输入一个证整数数组,判断该数组是不是二叉搜索树的后序遍历的结果,是则返回true,否则返回false,假设输入的数组的任意两个数字都不相同。
顺便复习一下二叉搜索树;
主要有4个特征;
(1)各个节点关键字不同
(2)左节点小于根结点
(3)右结点大于根结点
(4)根节点的左右子树分别是二叉搜索树
class Solution {
public:
bool VerifySquenceOfBST(vector<int> sequence) {
vector<int >lefttree,righttree;
int length=sequence.size();
int root=sequence[length-1];
if(sequence.empty()||length<=0)
return false;
int i=0;
for( i=0;i<length-1;i++)
{
if(sequence[i]>root)
break;//找到了第一个大于根结点的位置。返回i
}
for(int j=i;j<length-1;j++)
{
if(sequence[j]<root)
return false;//如果在下标为j---length处的数值有小于root的,就返回错误
}
if(i!=0)
{
for(int k=0;k<i;++k)
{
lefttree.push_back(sequence[k]);
}
}
if(i!=length-2)
{
for(int l=i;l<length-2;++l)
{
righttree.push_back(sequence[l]);
}
}
bool left=true;
bool right=true;
if(lefttree.size()>1)
VerifySquenceOfBST(lefttree);
if(righttree.size()>1)
VerifySquenceOfBST(righttree);
return (left&&right);
}
};