Leetcode Interview Question 26. Tree Substructure & Leetcode 617. Merging Binary Tree & Leetcode Interview Question 28. Symmetric Binary Tree

Leetcode Interview Question 26. Tree Substructure

Problem Description

Enter two binary trees A and B to determine whether B is a substructure of A. (It is agreed that an empty tree is not a substructure of any tree)
B is a substructure of A, that is, A has the same structure and node value as B.

Problem solving report

B is a sub-structure of A, there are three cases:

  • The root node of B is the same as the root node of A, but after forking, B stops first; this case corresponds to the helper()function.
  • B is the substructure of A's left subtree; this case corresponds to the isSubStructure()function.
  • B is the substructure of A's right subtree; this case corresponds to the isSubStructure()function.

Implementation code

/**
 * 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 isSubStructure(TreeNode* A, TreeNode* B) {
        if(A==NULL||B==NULL){
            return false;
        }
        if(B->val==A->val){
            return helper(A->left,B->left)&&helper(A->right, B->right);
        }
        else{
            return isSubStructure(A->left,B)||isSubStructure(A->right, B);
        }
    }
    bool helper(TreeNode* A, TreeNode* B){
        // 边界条件
        if(A==NULL||B==NULL){
            return B==NULL?true:false;
        }
        
        if(A->val==B->val){
            return helper(A->left,B->left)&&helper(A->right, B->right);
        }
        else{
            return false;
        }
        return true;
    }
};

References

[1] Leetcode Interview Question 26. Substructure of Tree

Leetcode 617. Merge binary tree

Problem Description

Given two binary trees, imagine that when you overlay one of them on the other, some nodes of the two binary trees will overlap.
You need to merge them into a new binary tree. The rule of merging is that if two nodes overlap, their values ​​are added as the new value after the nodes are merged, otherwise the node that is not NULL will be directly used as the node of the new binary tree.

Problem solving report

  • When the same node of both trees is not empty , we push them on the stack;
  • When the subsequent node of the left tree [corresponding to the tree on the right] is empty, direct its follow-up to the subsequent node of the right tree;
  • When the subsequent node of the left tree [corresponding to the tree on the right] is not empty, and the subsequent node of the corresponding node on the right is empty, it is not processed.

Implementation code

Non-recursive

/**
 * 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* mergeTrees(TreeNode* t1, TreeNode* t2) {
		if (t1 == NULL) return t2;
		if (t2 == NULL) return t1;
		stack<TreeNode*>s;
		s.push(t1);
		s.push(t2);
		while (!s.empty()){
			TreeNode* s2 = s.top();
			s.pop();
			TreeNode* s1 = s.top();
			s.pop();
			s1->val = s1->val + s2->val;
			if (s1->left == NULL) s1->left = s2->left;
			else if (s1->left != NULL && s2->left != NULL){
				s.push(s1->left);
				s.push(s2->left);
			}
			if (s1->right == NULL) s1->right = s2->right;
			else if (s1->right != NULL && s2->right != NULL){
				s.push(s1->right);
				s.push(s2->right);
			}
		}
		return t1;
	}
};

Recursive

class Solution {
public:
    TreeNode* mergeTrees(TreeNode* t1, TreeNode* t2)
    {
        if(!t1)
            return t2;
        if(!t2)
            return t1; 
        t1->val += t2->val;
        t1->left = mergeTrees(t1->left, t2->left);
        t1->right = mergeTrees(t1->right, t2->right);
        return t1;
    }
};

References

Leetcode Interview Question 28. Symmetric Binary Tree

Problem Description

Please implement a function to determine whether a binary tree is symmetrical. If a binary tree is the same as its mirror image, then it is symmetrical.
For example, the binary tree [1,2,2,3,4,4,3]is symmetrical. But it [1,2,2,null,3,null,3]is not symmetrical.

Problem solving report

Analyzing the recursive left and right subtrees is symmetrical.
The symmetry of the left and right subtrees must satisfy the following three conditions:

  • The value of the root node of the left subtree is equal to the value of the root node of the right subtree;
  • The left subtree of the left subtree and the right subtree of the right subtree are symmetrical;
  • The right subtree of the left subtree and the left subtree of the right subtree are symmetrical.

Implementation code

Non-recursive

class Solution{
    public:
        bool isSymmetric(TreeNode *root){
            TreeNode *left, *right;
            if(root==NULL) return true;
            queue<TreeNode*>q;
            q.push(root->left);
            q.push(root->right);
            while(!q.empty()){
                left=q.front();
                q.pop();
                right=q.front();
                q.pop();
                if(left==NULL&&right==NULL) continue;
                if(left==NULL||right==NULL) return false;
                if(left->val != right->val) return false;

                q.push(left->left);
                q.push(right->right);

                q.push(left->right);
                q.push(right->left);
            }
            return true;
        }
};

Recursive

class Solution {
public:
    bool isSymmetric(TreeNode* root) {
        if(root!=NULL){
            return helper(root->left, root->right);
        }
        return true;
    }
    bool helper(TreeNode*left, TreeNode*right){
        if(left==NULL&&right==NULL) return true;
        if(left==NULL||right==NULL) return false;
        if(left->val != right->val) return false;
        return helper(left->left, right->right)&&helper(left->right,right->left);
    }
};

References

[1] Leetcode interview question 28. Symmetric binary tree
[2] Problem solving area: Don Quixote and Sancho

MD_
Published 139 original articles · praised 8 · 10,000+ views

Guess you like

Origin blog.csdn.net/qq_27690765/article/details/105344672