您的位置:首页 > 其它

【树】二叉树的镜像(递归)、树的子结构(递归)难、从上往下打印二叉树(层次遍历)、二叉搜索树的后序遍历序列(递归)难、二叉树的深度(递归)

2019-03-16 20:54 141 查看

题目描述

操作给定的二叉树,将其变换为源二叉树的镜像。

输入描述:

[code]二叉树的镜像定义:源二叉树
8
/  \
6   10
/ \  / \
5  7 9 11
镜像二叉树
8
/  \
10   6
/ \  / \
11 9 7  5

/*
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;
    TreeNode *t=pRoot->left;
    pRoot->left=pRoot->right;
    pRoot->right=t;
    Mirror(pRoot->left);
    Mirror(pRoot->right);
}
};

 

题目描述

输入两棵二叉树A,B,判断B是不是A的子结构。(ps:我们约定空树不是任意一个树的子结构)

1、在A中找到和B的根结点值一样的节点R;2、判断树A中以R为根节点的子树是否包含树B一样的结构

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
public:
 bool Judge(TreeNode* pRoot1, TreeNode* pRoot2)
{
       if(pRoot2==NULL)
        return true;
    if(pRoot1==NULL)
        return false;
    if(pRoot1->val!=pRoot2->val)
    {
        return false;
    }
    return Judge(pRoot1->left, pRoot2->left)&&Judge(pRoot1->right, pRoot2->right);
}
bool HasSubtree(TreeNode* pRoot1, TreeNode* pRoot2)
{
    bool result=false;
    if(pRoot1!=NULL&&pRoot2!=NULL)
    {
        if(pRoot1->val==pRoot2->val)
        {
            result=Judge(pRoot1,pRoot2);
        }
        if(result==false)
        {
            result=HasSubtree(pRoot1->left,pRoot2);
        }
        if(result==false)
        {
            result=HasSubtree(pRoot1->right,pRoot2);
        }
    }
    return result;
}

};

 

题目描述

从上往下打印出二叉树的每个节点,同层节点从左至右打印。

/*
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>v;
    if(root==NULL)   //注意考虑特殊情况!!!
        return v;
    queue<TreeNode*>q;
    q.push(root);
    while(!q.empty())
    {
        TreeNode *t=q.front();
        q.pop();  //不要漏掉呀
        v.push_back(t->val);
        if(t->left)
        {
            q.push(t->left);
        }
        if(t->right)
        {
            q.push(t->right);
        }
    }
    return v;
}
};

 

题目描述

输入一个整数数组,判断该数组是不是某二叉搜索树的后序遍历的结果。如果是则输出Yes,否则输出No。假设输入的数组的任意两个数字都互不相同。

规律!!!:以数组{5,7,6,9,11,10,8}为例,后序遍历结果的最后一个数字8就是根节点的值,在这个数组中,5,7,6都比8小,是8的左子树;后三个数字9,11,10都比8大,是8的右子树;接下来用同样的方法确定与数组每部分对应的子树的结构,这其实是一个递归的过程。对于5,7,6,最后一个数字6是左子树的根节点的值,5比6小,是6的左子节点,7比6大,是6的右子节点。

再来分析数组{7,4,6,5},后序遍历的最后一个数5是根节点,因为7比5大,所以根节点5没有左子树,7、4、6都是5的右子树节点,因为4比5小,所以不存在一棵二叉搜索树。

class Solution {
public:
 bool Verify(vector<int>sequence,int begin,int end,int len)
{
    if(sequence.size()==0||len<=0)
        return false;
    int root=sequence[len-1];
    int i=0;
    for(i=0;i<len-1;i++)
    {
        if(sequence[i]>root)
            break;
    }
    int j=i;
    for(j=i;j<len-1;j++)
    {
        if(sequence[j]<root)
            return false;
    }
    bool t1=true;
    bool t2=true;
    if(i>0)
    t1=Verify(sequence,0,i-1,i);
    if(i<len-1)
    t2=Verify(sequence,i+1,len-1,len-i-1);
    return t1&&t2;
}
bool VerifySquenceOfBST(vector<int> sequence) {
    int len=sequence.size();
    return Verify(sequence,0,len-1,len);
}
};

 

题目描述

输入一棵二叉树,求该树的深度。从根结点到叶结点依次经过的结点(含根、叶结点)形成树的一条路径,最长路径的长度为树的深度。

/*
struct TreeNode {
    int val;
    struct TreeNode *left;
    struct TreeNode *right;
    TreeNode(int x) :
            val(x), left(NULL), right(NULL) {
    }
};*/
class Solution {
public:
int TreeDepth(TreeNode* pRoot)
{
    if(pRoot==NULL)。//写递归时,一定要有结束条件
        return 0;
    return max(TreeDepth(pRoot->left)+1,TreeDepth(pRoot->right)+1);
}
};

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐