LeetCode-Binary tree OJ question

1. Single value binary tree 

965. Univalued Binary Treeicon-default.png?t=N7T8https://leetcode.cn/problems/univalued-binary-tree/

 First determine whether the tree is empty. If it is an empty tree, it is true. Then determine whether the left subtree is empty, and the value val of the left subtree is different from the val of the current node. If the left subtree is not empty and val is not equal to the val of the root, false is returned, and then the right subtree is determined using the same method. Tree. Finally, recurse the left and right subtrees. If only one of the left and right subtrees returns false, then the whole returns false.

bool isUnivalTree(struct TreeNode* root) 
{
    if(root==NULL)
        return true;
    if(root->left&&root->left->val!=root->val)
    {
        return false;
    }
    if(root->right&&root->right->val!=root->val)
    {
        return false;
    }
    return isUnivalTree(root->right)&&isUnivalTree(root->left);

}


2. Find the maximum depth of the binary tree 

104. Maximum depth of binary treeicon-default.png?t=N7T8https://leetcode.cn/problems/maximum-depth-of-binary-tree/

 

First, the empty test is performed and 0 is returned. Then declare two variables m and n to represent the left and right depths of the tree respectively, and use recursion here to assign their left and right subtrees respectively, which can play a traversal role (search). We then use trinocular judgment to assign a value to it, and take the higher one, because the maximum depth of the tree is the height of the root, so here we also need to add one at the end, that is, to supplement the height from the root subtree to the root.

int maxDepth(struct TreeNode* root) 
{
    int m=0;
    int n=0;
    if(root==NULL)
    {
        return 0;
    }
    else
    {
        m=maxDepth(root->left);
        n=maxDepth(root->right);
    }
    return m>n?m+1:n+1;
    
}

 3. Flip the binary tree

226. Invert Binary Treeicon-default.png?t=N7T8https://leetcode.cn/problems/invert-binary-tree/

 To flip a binary tree, first save the left and right subtrees of the current node, then place the saved left subtree left in the right position, and then recurse the left and right subtrees.

struct TreeNode* invertTree(struct TreeNode* root) 
{
    if(root==NULL)
        return NULL;
    struct TreeNode* left=root->right;
    struct TreeNode* right=root->left;
    root->left=left;
    root->right=right;
    invertTree(root->left);
    invertTree(root->right);
    return root;
}

4. Mirror binary tree 

101. Symmetric Binary Treeicon-default.png?t=N7T8https://leetcode.cn/problems/symmetric-tree/

The question is actually very similar to the same binary tree question. We can directly copy that code and use it. The explanation of this question is athttp://t. csdnimg.cn/ArwMx

The difference lies in the final recursion. If the same tree is compared with the left subtree and the right subtree, then the mirrored binary tree is compared with the left subtree and the right subtree.​ 

bool isSymmetricTree(struct TreeNode* p,struct TreeNode* q)
{
    //都为空
    if(q==NULL&&p==NULL)
        return true;
    //一个为空
    if(q==NULL||p==NULL)
        return false;
    if(q->val!=p->val)
        return false;
    return isSymmetricTree(p->left,q->right)&&isSymmetricTree(p->right,q->left);
}
bool isSymmetric(struct TreeNode* root) 
{
    return isSymmetricTree(root->left,root->right);
}

Guess you like

Origin blog.csdn.net/2301_79035870/article/details/134811505