Binary tree brushing practice (2)


foreword


insert image description here

Continuing from the previous article, this article is still the basic oj question of the binary tree

1. The same tree

1. Topic introduction

topics in the same tree
insert image description here

2. Ideas

The idea of ​​this question is very simple, just traverse the two trees once (this question must be able to be done independently, and the following questions are derived from this question)

3. Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */


bool isSameTree(struct TreeNode* p, struct TreeNode* q){
    
    
     if(p==NULL&&q==NULL)
    {
    
    
        return true;
    }
    if(p==NULL||q==NULL)
    {
    
    
        return false;
    }
    if(p->val!=q->val)
    {
    
    
        return false;
    }
    return isSameTree(p->left,q->left)&&isSameTree(p->right,q->right);
}

2. Symmetric binary tree

1. Topic introduction

The topic is in a symmetric binary tree
insert image description here

2. Ideas

Symmetric binary tree is an advanced question of the previous question, that is, it is axisymmetric, and recursion is a divide-and-conquer idea. We can find the law of a whole tree from a small number of numbers. As shown in the figure, we find that we only need to check Whether the right subtree of root's left subtree is equal to the left subtree of root's right subtree, and whether the left subtree of root's left subtree is equal to the right subtree of root's right subtreeinsert image description here

3. Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
 bool PreOrder(struct TreeNode* p, struct TreeNode* q)
{
    
    
    if(p==NULL&&q==NULL)
    return true;
    if(p==NULL||q==NULL)
    {
    
    
        return false;
    }
    if(p->val!=q->val)
    return false;
	return PreOrder(p->left,q->right)&&PreOrder(p->right,q->left);//&&只要有一个false就是flase了,所以上面都是讨论false的情况
}
bool isSymmetric(struct TreeNode* root){
    
    
    return PreOrder(root->left,root->right);
}

3. A subtree of another tree

1. Topic introduction

Subject is in a subtree of another tree
insert image description here

2. Ideas

This question is based on the combination of the first question. The question says a subtree of a tree. Isn’t that just to find out whether the subtree of a number is the same as this tree? Let’s traverse it once and take each node Compare the root with the subroot, if they are the same, we will judge whether the tree rooted at this node is the same as the subroot

3. Code

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */
bool judge(struct TreeNode* p,struct TreeNode* q)//判断两棵树是不是完全相同,基本续用第一题代码
{
    
    
    if(p==NULL&&q==NULL)
    {
    
    
        return true;
    }
    if(p==NULL||q==NULL)
    {
    
    
        return false;
    }
    if(p->val!=q->val)
    {
    
    
        return false;
    }
    return judge(p->left,q->left)&&judge(p->right,q->right);
}

bool isSubtree(struct TreeNode* root, struct TreeNode* subRoot){
    
    
    if(root==NULL)
    {
    
    
        return false;
    }
    if(root->val==subRoot->val&&judge(root,subRoot))
    {
    
    
        return true;
    }
    return isSubtree(root->left,subRoot)||isSubtree(root->right,subRoot);

}

Summarize

The basic part of the binary tree oj question is basically finished, and I will continue to update the more difficult questions of the binary tree later. If you look forward to it, please vote for me (

Guess you like

Origin blog.csdn.net/Ruiren_/article/details/129903294