Leetcode 1367. Linked List in Binary Tree (二叉树好题)

  1. Linked List in Binary Tree
    Medium
    Given a binary tree root and a linked list with head as the first node.

Return True if all the elements in the linked list starting from the head correspond to some downward path connected in the binary tree otherwise return False.

In this context downward path means a path that starts at some node and goes downwards.

Example 1:

Input: head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Explanation: Nodes in blue form a subpath in the binary Tree.
Example 2:

Input: head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: true
Example 3:

Input: head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
Output: false
Explanation: There is no path in the binary tree that contains all the elements of the linked list from head.

Constraints:

The number of nodes in the tree will be in the range [1, 2500].
The number of nodes in the list will be in the range [1, 100].
1 <= Node.val <= 100 for each node in the linked list and binary tree.

解法1:
这题其实并不容易。要在整个二叉树里面,对每个节点调用helper()函数,用前中后序遍历应该都可以。helper()则是用的分解问题的方法。

/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
   
public:
    bool isSubPath(ListNode* head, TreeNode* root) {
   
        if (!head) return true;
        if (!root) return false;
        if (helper(head, root)) return true;
        return isSubPath(head, root->left) || isSubPath(head, root->right);  
    }
private:
    bool helper(ListNode* head, TreeNode* root) {
   
        if (!head) return true;
        if (!root) return false;
        if (head->val != root->val) return false;
        return helper(head->next, root->left) || helper(head->next, root->right);
    }
};

我一开始写成下面这样,但是不对。因为它只调用了一次helper(),如果链表是{2,2,1},而树里面存在一个path{2,2,2,1},结果应该返回true。但这个解法里面,读到第3个2的时候,发现不对,已经没法走回头路了。
应该在整个树里面,对每个节点都调用helper(),用前/中/后序遍历都可以。

class Solution {
   
public:
    bool isSubPath(ListNode* head, TreeNode* root) {
   
        origHead = head;
        origRoot = root;
        helper(head, root);
        return gPathExist;
    }
private:
    bool gPathExist = false;
    TreeNode *origRoot = NULL;
    ListNode *origHead = NULL;
    void helper(ListNode *head, TreeNode *root) {
   
        if (!head) {
   
            gPathExist = true;
            return;
        }
        if (!root || gPathExist) return;
        if (root->val == head->val) {
   
            helper(head->next, root->left);
            helper(head->next, root->right);
        } else {
   
            if (root == origRoot) {
   
                helper(origHead, root->left);
                helper(origHead, root->right);
            } else if (head != origHead) {
   
                helper(origHead, root);
                helper(origHead, root);
            }
        }
    }
};

相关推荐

  1. Leetcode 1367. Linked List in Binary Tree ()

    2024-01-10 08:14:01       31 阅读
  2. Leetcode 993. Cousins in Binary Tree (遍历)

    2024-01-10 08:14:01       28 阅读
  3. Leetcode 437. Path Sum III (遍历)

    2024-01-10 08:14:01       32 阅读
  4. Leetcode 998. Maximum Binary Tree II (构建)

    2024-01-10 08:14:01       36 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-10 08:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-10 08:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-10 08:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-10 08:14:01       20 阅读

热门阅读

  1. 笔记:ubuntu22.04重启后无法启动网络

    2024-01-10 08:14:01       35 阅读
  2. nacos和openFeign

    2024-01-10 08:14:01       23 阅读
  3. docker 安装redis集群

    2024-01-10 08:14:01       38 阅读
  4. CPU控制的独立式键盘扫描实验

    2024-01-10 08:14:01       28 阅读
  5. Qt UI框架和Duilib UI框架差别

    2024-01-10 08:14:01       33 阅读
  6. 7个Linux搜索和过滤命令

    2024-01-10 08:14:01       34 阅读
  7. C++ 中关键字 Static

    2024-01-10 08:14:01       40 阅读
  8. vue day06

    vue day06

    2024-01-10 08:14:01      38 阅读
  9. 梯度提升机(Gradient Boosting Machines,GBM)

    2024-01-10 08:14:01       56 阅读
  10. Android 8.1 默认应用加入系统白名单

    2024-01-10 08:14:01       38 阅读
  11. Leetcode15-最大字符串配对数目(2744)

    2024-01-10 08:14:01       35 阅读
  12. Excel使用pandas拆分单元格扩展

    2024-01-10 08:14:01       38 阅读
  13. windows配置电脑网络IP的方法

    2024-01-10 08:14:01       35 阅读
  14. 在Linux中tomcat执行shutdown.sh之后进程还存在

    2024-01-10 08:14:01       34 阅读
  15. 在Linux中tomcat出现乱码

    2024-01-10 08:14:01       35 阅读