---- OFFERs prove safety substructure surface 26. tree questions

Links: https://leetcode-cn.com/problems/shu-de-zi-jie-gou-lcof/

 

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 isPart(TreeNode* A, TreeNode* B) {
        if (A == NULL || B == NULL) {
            return B == NULL ? true : false;
        }
        if (A->val != B->val) {
            return false;
        }
        return isPart(A->left, B->left) && isPart(A->right, B->right);
    }
    bool isSubStructure(TreeNode* A, TreeNode* B) {
        if (A == NULL || B == NULL) {
            return false;
        }
        return isPart(A, B) || isSubStructure(A->left, B) || isSubStructure(A->right, B);
    }
};

 

Guess you like

Origin www.cnblogs.com/clown9804/p/12363358.html