LeetCode Brushing Notes-Data Structure-day18

LeetCode Brushing Notes-Data Structure-day18

236. Nearest Common Ancestor of Binary Tree

1. Description of the topic

Original title link: 236. The nearest common ancestor of the binary tree

image-20220202104534978

image-20220202104547515

2. Problem-solving ideas

algorithm:DFS

pThe nearest common ancestor of and qcan be divided into these situations:

  1. If the current node root == p, it means that point q must be in one of the left and right subtrees of root, and the nearest public node must be p
  2. If the current node root == q, it means that point p must be in one of the left and right subtrees of root, and the nearest public node must be q
  3. If neither of 1 and 2 is true, then the nearest common ancestor of p and q is either in the left subtree of root or in the right subtree of root, then recurse directly to root->left and root->right to search. If after the recursion is complete, the left subtree returns null to indicate that it was not found, then the answer must be in the right subtree. Similarly, if the right subtree returns null to indicate that it was not found, then the answer must be in the left subtree
  4. If the left and right subtrees in the 3 cases cannot find the nearest common ancestor of p and q, it means that p and q are in different left and right subtrees respectively, then root is their nearest common ancestor, and returns directly to root

3. Code

class Solution {
    
    
    public:
    TreeNode* lowestCommonAncestor(TreeNode* root, TreeNode* p, TreeNode* q) {
    
    
        if(!root) return NULL;
        if(root==p||root==q) return root;
        TreeNode* left=lowestCommonAncestor(root->left,p,q);
        TreeNode* right=lowestCommonAncestor(root->right,p,q);
        if(!left) return right;
        if(!right) return left;
        return root;
    }
};

297. Serialization and deserialization of binary tree

1. Description of the topic

Original title link: 297. Serialization and deserialization of binary trees

image-20220202104618337

image-20220202104656775

2. Problem-solving ideas

algorithm:DFS

  1. #Serialization: save the sequence of the entire binary tree for pre-order traversal, and at the same time need to mark the empty nodes of each node with
  2. Deserialization: When deserializing, according to ,the separation, construct the left and right subtrees recursively after constructing the current node

It is recommended to refer to the official solution: official solution

3. Code

class Codec {
    
    
public:
    string path;
    // Encodes a tree to a single string.
    string serialize(TreeNode* root) {
    
    
        dfsD(root);
        return path;
    }
    void dfsD(TreeNode* root){
    
    
        if(!root){
    
    
            path+="#,";
        }else{
    
    
            path+=to_string(root->val)+",";
            dfsD(root->left);
            dfsD(root->right);
        }
    }

    // Decodes your encoded data to tree.
    TreeNode* deserialize(string data) {
    
    
        int u=0;
        return dfsS(data,u);

    }
    TreeNode* dfsS(string& data,int& u){
    
    
        if(data[u]=='#'){
    
    
            u+=2;
            return NULL;
        }
        int k=u;
        while(data[u]!=',') u++;
        TreeNode* t=new TreeNode(stoi(data.substr(k,u-k)));
        u++;
        t->left=dfsS(data,u);
        t->right=dfsS(data,u);
        return t;
    }
};

insert image description here

Guess you like

Origin blog.csdn.net/qq_45966440/article/details/122774405