leetcode (Data Structure) - binary image

Mirror binary tree, power button above the title, this question is very simple, put out the reasons is that it requires using two solutions to write this question - recursive and iterative, but also learned a tree data structure, the recording process of their own learning lest I forget, no place to look for.

Topic intention is clear, is that you write a program and then see if it is symmetrical, symmetrical conditions are obvious:

// left subtree right subtree point value equal to the value of       
        LeftChild -> == RightChild- Val> Val
    

  We think about what kind of tree is then called mirror symmetry?

When a tree is not left subtree and right subtree mirror symmetry, then the tree is symmetrical. So the question is not can be converted into: two tree mirror each other under what circumstances?

  It is clear

When the sub-tree should be consistent with each other symmetrically about the conditions:

  1, two root thereof have the same value.

  2, the left and right image sub-tree to another tree and subtrees are symmetric to each tree.

//力扣给的结构体
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     struct TreeNode *left;
 *     struct TreeNode *right;
 * };
 */

; Now, let me think about how to achieve recursive

  1, there will be a recursive termination conditions, then this represents a recursive function when the end of it?

    Of course, it is to reach a maximum depth of this branch of the tree. So how to detect recursion maximum depth of the tree branches, it is clear that every binary tree, if it is not the root, then, when the left and right subtrees of the node are empty, said this one will reveal all the details of this tree a.

    Since this is a binary image instead of measuring the depth of the problem, so we returned, there are three conditions:

    The first, two nodes are empty, return true;

    Second, no two nodes are empty, return false;

    Third, two nodes values ​​are not equal, return false;

We come to the termination condition recursion basic shape, leaving the iteration, iteration of this relatively simple, not repeat them here.

Paste the code:

  

bool CheckVal(struct TreeNode* LeftChild, struct TreeNode* RightChild)
{
    if (NULL == LeftChild && NULL == RightChild)
    {
        return true;
    }
    if (NULL == LeftChild || NULL == RightChild)
    {
        return false;
    }
    return (LeftChild->val == RightChild->val) && CheckVal(LeftChild->left, RightChild->right) && CheckVal(LeftChild->right, RightChild->left);
}

bool isSymmetric(struct TreeNode* root){
    if (NULL == root)
    {
        return true;
    }
    return CheckVal(root->left, root->right);
}

  Iteration not do elaborate, this is too simple to implement, BFS slight change to change on the line

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

bool isSymmetric(struct TreeNode* root){
    struct TreeNode *Queue[MAXSIZE];
    int index = 0;
    Queue[index++] = root;
    Queue[index++] = root;
    
    while (0 != index)
    {
        struct TreeNode *leftChild = Queue[--index];
        struct TreeNode *rightChild = Queue[--index];
        
        if (NULL == leftChild && NULL == rightChild) 
        {
            continue;
        }
        if ((NULL == leftChild || NULL == rightChild) || (leftChild->val != rightChild->val))
        {
            return false;
        }
        
        Queue[index++] = leftChild->left;
        Queue[index++] = rightChild->right;
        Queue[index++] = leftChild->right;
        Queue[index++] = rightChild->left;
    }
    return true;
}

  Well, I like this little stack.

Algorithm is not easy, gentlemen encourage each other!

Guess you like

Origin www.cnblogs.com/daker-code/p/11719147.html