【leetcode】叶子相似的树

 

bool recursion(struct TreeNode* root, int* arr, int* pst)
{
    if (!root->left && !root->right)
    {
        if (!arr[*pst])
        {
            arr[(*pst)++] = root->val;
            return true;
        }
        else
        {
            return (arr[(*pst)++] == root->val)? true: false;
        }
    }
    else if (!root->left) return recursion(root->right,arr,pst);
    else if (!root->right) return recursion(root->left,arr,pst);
    return recursion(root->left,arr,pst) && recursion(root->right,arr,pst);
}

bool leafSimilar(struct TreeNode* root1, struct TreeNode* root2){
    int arr[1000]={0};
    int pst=0,num=0;
    recursion(root1,arr,&pst);
    return (recursion(root2,arr,&num) && pst==num)?true : false;
}

 

posted @ 2020-09-23 10:58  温暖了寂寞  阅读(153)  评论(0编辑  收藏  举报