[Swordfinger Offer] Search and Backtrace Algorithms

Keywords: Algorithm data structure leetcode

Today is still the application of search algorithm, but today there are more depth-first search algorithms, the data structure is still a tree, involves more knowledge of recursion, so the code volume is less.

Swordfinger Offer 26.Substructure of Trees

Enter two binary trees A and B to determine if B is a substructure of A. (A convention empty tree is not a substructure of any tree)

B is the substructure of A, that is, A has the same structure and node values as B.

Example 1:

Input: A = [1,2,3], B = [3,1]
Output: false

Example 2:

Input: A = [3,4,5,1,2], B = [4,1]
Output: true

Restrictions:

0 <= Number of nodes <= 10000
/**
 * 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 isSim(TreeNode* A, TreeNode* B){
        if(B == nullptr)return true;
        if(A == nullptr)return false;
        if(A->val != B->val)return false;
        return isSim(A->left, B->left) && isSim(A->right, B->right);
    }
    bool isSubStructure(TreeNode* A, TreeNode* B) {
        if(B == nullptr || A == nullptr)return false;
        if(A->val == B->val){
            if(isSim(A, B) == true)return true;
        }
        return isSubStructure(A->left, B) || isSubStructure(A->right, B);
    }
};

Algorithmic ideas:

This question mainly refers to tree traversal. First, traverse the tree to find the nodes that may have the same structure as B (t->val == B->val)Then another traversal function is enabled at this point to determine if B is isomorphic, whether it is isomorphic is recursive, whether the root node is the same, and whether the left and right subtrees are isomorphic. Knowing that B is empty or A is empty or the Vals of the child nodes are no longer the same, because it only determines whether B is a substructure of A, so when B traverses to nullptr it has been shown that A can match all the nodes before B.You can return true.

Swordfinger Offer 27.Mirror of Binary Tree

Please complete a function, enter a binary tree, which outputs its mirror.

Example 1:

Input: root = [4,2,7,1,3,6,9]
Output:[4,7,2,9,6,3,1]

Restrictions:

0 <= Number of nodes <= 1000
/**
 * 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:
    TreeNode* mirrorTree(TreeNode* root) {
        if(root == nullptr)return nullptr;
        TreeNode* tmp = root->left;
        root->left = mirrorTree(root->right);
        root->right = mirrorTree(tmp);
        return root;
    }
};

Algorithmic ideas:

This question is still recursive, mirroring the left and right subtrees in turn, and then swapping the left and right subtrees, you can complete the mirroring of the whole tree.

Sword Finger Offer 28.Symmetric Binary Tree

Implement a function to determine if a binary tree is symmetrical. If a binary tree is the same as its mirror, then it is symmetrical.

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

Restrictions:

0 <= Number of nodes <= 1000
/**
 * 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 dfs(TreeNode* a, TreeNode *b){
        if(a == nullptr && b == nullptr)return true;
        if(a == nullptr || b == nullptr)return false;
        if(a->val != b->val)return false;
        return dfs(a->left, b->right) && dfs(a->right, b->left);
    }
    bool isSymmetric(TreeNode* root) {
        if(root == nullptr)return true;
        return dfs(root->left, root->right);
    }
};

Algorithmic ideas:

To judge whether a and B are symmetrical, the first step is to judge whether a->val, b->val are equal, the second step is to judge whether the left subtree of a and the right subtree of B are symmetrical, the right subtree of a and the left subtree of a are symmetrical, which is equivalent to two mirror traversals at the same time, so the recursive equation can be easily written out.


[Swordfinger Offer] Series:
Swordfinger Offer stack
Chain List
[Swordfinger Offer] String
[Swordfinger Offer] Find Algorithm
[Swordfinger Offer] Search algorithm (1)
[Swordfinger Offer] Search and Backtrace Algorithms

Posted by hws on Wed, 15 Sep 2021 17:29:17 -0700