leetcode 1367 Linked List in Binary Tree

isSubFrom(head,root)判断从root开始,是否存在链表中的数字。

class Solution {
public:
    bool isSubPath(ListNode* head, TreeNode* root) {
        if(!head) return true;
        if(!root) return false;
        return isSubFrom(head,root)||isSubPath(head,root->left)||isSubPath(head,root->right);//判断当前root节点是否存在链表中的数字;如果不存在,则判断从其左子树开始是否存在;如果不存在,判断从其右子树开始是否存在。
    }
    
    bool isSubFrom(ListNode* head,TreeNode* root) {
        if(!head) return true;
        if(!root||head->val!=root->val) return false;
        return isSubFrom(head->next,root->left)||isSubFrom(head->next,root->right);
    }
};

 

posted @ 2020-04-12 14:29  qiujiejie  阅读(105)  评论(0编辑  收藏  举报