2 face questions: symmetrical binary tree (C ++)

Topic Address: https://leetcode-cn.com/problems/dui-cheng-de-er-cha-shu-lcof/

Title Description

Please implement a function, a binary tree is used to determine not symmetrical. If a binary tree and its image, as it is symmetrical.

For example, a binary tree [1,2,2,3,4,4,3] is symmetric.

    1
   / \
  22
 / \ / \
3443
but the following [1,2,2, null, 3, null , 3] is not a mirror image:

    1
   / \
  2   2
   \   \
   3    3

Examples of topics

Example 1:

Input: root = [1,2,2,3,4,4,3]
Output: true
Example 2:

Input: root = [1,2,2, null, 3, null, 3]
Output: false

Problem-solving ideas

DFS

(1) Recursive: recursive method determines whether the left and right subtrees of the same value, particularly if the divided left and right subtrees are empty subtree, left subtree and right subtree is complete, and the left and the right child subtree several tree values are equal, when left and right subtrees are equal, a left node and a right node of the right subtree Comparative left subtree, and the left and right node of the node left subtree and right subtree.

(2) non-recursive - Stack : using two pointers p and q traversing left subtree and right subtree, wherein the pointer p has been traversed left traversal order of the left subtree root node -> the left node -> the right node pointer q has the right, the order of traversal of the right subtree root node -> the right node -> the left node

BFS

Queue : queue using the iterative, because binary tree is a mirror image, so the queue in which two consecutive nodes should be equal, to achieve this function, we need to load queue element, the first node in the left into the left subtree, right node then charged right subtree, the nodes and the left and right and left subtree node right subtree, the final judgment, when the two nodes are empty, then the mirror satisfies the conditions described, the loop continues for an image nodes, or when one of the nodes is empty, returns false, this time can not be described mirror configuration.

Program source code

Recursion

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true; //判空
        return isMirror(root->left, root->right);
    }
    bool isMirror(TreeNode* left, TreeNode* right)
    {
        IF (&& nullptr a left right == == nullptr a) return  to true ; // left and right subtrees are empty, return to true 
        IF (left right == == nullptr a || nullptr a) return  to false ; // left subtree has a is empty, false is returned 
        IF (lEFT -> Val = right-> Val!) return  to false ; // left and right subtrees values are not equal, returns to false 
        return isMirror (lEFT -> left, right-> right) && isMirror (left -> right, right-> left); // time of approximately equal sub-tree, comparing the left and right subtree node left and right subtree of the node, and left node and right node and left subtree right subtree 
    }
};

Stack

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        stack<TreeNode*> s1, s2;
        TreeNode* p = root;
        The TreeNode * Q = the root;
         the while (!! P = nullptr a || s1.empty ()) {
             the while (! P = nullptr a) {
                 IF (! Q || nullptr a p-==> = Q- Val> Val) return  to false ; // left subtree one or two null pointer to the current node value is not equal to
                s1.push(p);
                s2.push(q);
                p = p->left; 
                q = q->right; 
            }
            IF (Q = nullptr a!) return  to false ; // P is empty, q is not empty, i.e., about one empty subtree
            p = s1.top(); 
            s1.pop();
            q = s2.top(); 
            s2.pop();
            p = p->right;
            q = q->left;
        }
       return true; 
    }
};

queue

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr) return true;
        queue<TreeNode*> que;
        que.push(root->left);
        que.push(root->right);
        while(!que.empty())
        {
            TreeNode* p = que.front();
            que.pop ();
            TreeNode* q = que.front();
            que.pop ();
            if(p == nullptr && q == nullptr) continue;    
            if(p == nullptr || q == nullptr) return false;
            if(p->val != q->val) return false;
            que.push(p->left);
            que.push(q->right);
            que.push(p->right);
            que.push(q->left);
        }
        return true;
    }
};

 

Guess you like

Origin www.cnblogs.com/wzw0625/p/12657538.html