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()则是用的分解问题的方法。

cpp 复制代码
/**
 * 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(),用前/中/后序遍历都可以。

cpp 复制代码
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);
            }
        }
    }
};
相关推荐
BingLin-Liu4 分钟前
蓝桥杯备考:红黑树与map和set
职场和发展·蓝桥杯
天乐敲代码30 分钟前
JAVASE入门九脚-集合框架ArrayList,LinkedList,HashSet,TreeSet,迭代
java·开发语言·算法
十年一梦实验室35 分钟前
【Eigen教程】矩阵、数组和向量类(二)
线性代数·算法·矩阵
Kent_J_Truman36 分钟前
【子矩阵——优先队列】
算法
快手技术2 小时前
KwaiCoder-23BA4-v1:以 1/30 的成本训练全尺寸 SOTA 代码续写大模型
算法·机器学习·开源
一只码代码的章鱼2 小时前
粒子群算法 笔记 数学建模
笔记·算法·数学建模·逻辑回归
小小小小关同学2 小时前
【JVM】垃圾收集器详解
java·jvm·算法
Swift社区3 小时前
统计文本文件中单词频率的 Swift 与 Bash 实现详解
vue.js·leetcode·机器学习
圆圆滚滚小企鹅。3 小时前
刷题笔记 贪心算法-1 贪心算法理论基础
笔记·算法·leetcode·贪心算法
Kacey Huang3 小时前
YOLOv1、YOLOv2、YOLOv3目标检测算法原理与实战第十三天|YOLOv3实战、安装Typora
人工智能·算法·yolo·目标检测·计算机视觉