Symmetric binary leetcode 101

Binary tree? symmetry!

Title:
Given a binary tree, check if it is mirror-symmetrical

Examples:
binary [1,2,2,3,4,4,3] symmetric
-. 1
- / - \
-2 ---- 2
- / - \ - / - \
34--43

Topic analysis

  • Binary symmetric
    about 1. subtrees ==> the same configuration as
    the left and right subtrees 2 ==> corresponding to the same location of the digital
Binary Symmetric ==> the same left and right subtrees

For the currentRoot

Left subtree of the left subtree right subtree right subtree same
right subtree of the left subtree of the left subtree right subtree of the same

which is
Recursion! !
Determining whether each node is the same to the left and right subtrees

Problem-solving ideas

function effect
judge(TreeNodea,TreeNodeb) Determine whether the same two binary tree

process:

  • Root two trees different values
    ==> return false

  • The root of two trees have no left and right subtrees (described recursive traversal to a leaf node)
    ==> return to true

  • Two trees root of the left and right subtrees are present
    ==>
    right subtree of the left subtree - right subtree of the left subtree same
    same right subtree right subtree - left subtree of the left subtree

    == > return true

  • Two trees root of the left and right sub-tree structure does not correspond
    ==>
    the presence of a left subtree of the tree and the other tree right subtree absence
    and presence of another tree has a tree right subtree left subtree does not exist

    ==> return false

  • Root of two trees left and right subtrees are present but no structural correspondence
    ==>
    presence left subtree of a tree to another tree right subtree corresponding to the presence of
    the presence of a tree to another tree right subtree corresponding to the presence of the left subtree

    ==> recursively determines whether the corresponding sub-tree of the same

code show as below

bool judge(TreeNode*a,TreeNode*b)
{
    if(a->val != b->val) return false;
    if(!a->right && !b->left && !a->left && !b->right) return true;     //递归遍历到叶子结点
    if(a->right && b->left&&a->left && b->right)
    {
        return (judge(a->right, b->left)&&judge(a->left, b->right));
    }
    if((a->right==NULL&& b->left)||(a->right&& b->left ==NULL) ||(a->left==NULL&& b->right)
    ||(a->left&& b->right ==NULL) ) return false;                      //a b树子树结构不对应
    if (a->right && b->left ) return judge(a->right, b->left);    //a树的右子树且b树的左子树存在
    if (a->left && b->right) return judge(a->left, b->right);     //a树的左子树且b树的右子树存在
    return false;
}
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(!root) return true;
        if(!root -> left && !root -> right) return true; 
        if(root -> left && root -> right) 
        return judge(root->left, root->right);
        else return false;
    }
};
Published 34 original articles · won praise 0 · Views 581

Guess you like

Origin blog.csdn.net/Luyoom/article/details/104139578