国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

Leetcode 1367. Linked List in Binary Tree (二叉樹好題)

這篇具有很好參考價(jià)值的文章主要介紹了Leetcode 1367. Linked List in Binary Tree (二叉樹好題)。希望對大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

  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:
這題其實(shí)并不容易。要在整個(gè)二叉樹里面,對每個(gè)節(jié)點(diǎn)調(diào)用helper()函數(shù),用前中后序遍歷應(yīng)該都可以。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);
    }
};

我一開始寫成下面這樣,但是不對。因?yàn)樗徽{(diào)用了一次helper(),如果鏈表是{2,2,1},而樹里面存在一個(gè)path{2,2,2,1},結(jié)果應(yīng)該返回true。但這個(gè)解法里面,讀到第3個(gè)2的時(shí)候,發(fā)現(xiàn)不對,已經(jīng)沒法走回頭路了。
應(yīng)該在整個(gè)樹里面,對每個(gè)節(jié)點(diǎn)都調(diào)用helper(),用前/中/后序遍歷都可以。文章來源地址http://www.zghlxwxcb.cn/news/detail-822298.html

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);
            }
        }
    }
};

到了這里,關(guān)于Leetcode 1367. Linked List in Binary Tree (二叉樹好題)的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 數(shù)據(jù)結(jié)構(gòu)與算法 | 二叉樹(Binary Tree)

    數(shù)據(jù)結(jié)構(gòu)與算法 | 二叉樹(Binary Tree)

    二叉樹(Binary Tree)是一種樹形數(shù)據(jù)結(jié)構(gòu),由節(jié)點(diǎn)構(gòu)成,每個(gè)節(jié)點(diǎn)最多有兩個(gè)子節(jié)點(diǎn):一個(gè)左子節(jié)點(diǎn)和一個(gè)右子節(jié)點(diǎn)。 \\\"二叉樹\\\"(Binary Tree)這個(gè)名稱的由來是因?yàn)槎鏄涞拿總€(gè)節(jié)點(diǎn)最多有兩個(gè)子節(jié)點(diǎn),一個(gè)左子節(jié)點(diǎn)和一個(gè)右子節(jié)點(diǎn)。其中,“二叉”指的是兩個(gè),因此“二叉樹

    2024年02月08日
    瀏覽(24)
  • 數(shù)據(jù)結(jié)構(gòu)第四天: Complete Binary Search Tree 【搜索二叉樹,完全二叉樹】

    數(shù)據(jù)結(jié)構(gòu)第四天: Complete Binary Search Tree 【搜索二叉樹,完全二叉樹】

    ?這道題需要的操作時(shí)排序并且需要遍歷,最重要的一點(diǎn)它是個(gè)完全二叉樹,所以數(shù)組是最適合的 這道題我的思路來自于浙江大學(xué)課件7.0.2完全二叉樹 這道題說白就是將輸入的樣例構(gòu)造成一個(gè)完全搜索二叉樹。因?yàn)橥耆€索二叉樹的 根節(jié)點(diǎn)一定是是一個(gè)處于左右子樹中間的那

    2024年02月06日
    瀏覽(28)
  • Java 【數(shù)據(jù)結(jié)構(gòu)】 二叉樹(Binary_Tree)【神裝】

    Java 【數(shù)據(jù)結(jié)構(gòu)】 二叉樹(Binary_Tree)【神裝】

    ? ? 登神長階 ?第五神裝 二叉樹 Binary-Tree 目錄 ???一.樹形結(jié)構(gòu) ??1.概念 ??2.具體應(yīng)用 ???二.二叉樹(Binary Tree) ??1.概念 ???2.表現(xiàn)形式 ??3.特殊類型 ??3.1完全二叉樹(Complete Binary Tree) ??3.2滿二叉樹(Full Binary Tree) ??4.性質(zhì)? ??5.二叉樹的遍歷 ??5.1前中后序遍歷

    2024年04月27日
    瀏覽(21)
  • Java學(xué)數(shù)據(jù)結(jié)構(gòu)(2)——樹Tree & 二叉樹binary tree & 二叉查找樹 & AVL樹 & 樹的遍歷

    Java學(xué)數(shù)據(jù)結(jié)構(gòu)(2)——樹Tree & 二叉樹binary tree & 二叉查找樹 & AVL樹 & 樹的遍歷

    1.樹的出現(xiàn):解決鏈表線性訪問時(shí)間太慢,樹的時(shí)間復(fù)雜度O(logN); 2.二叉樹的定義,最多兩個(gè)兒子節(jié)點(diǎn); 3.二叉查找樹,左小,右大,中居中;remove方法,兩種,只有一個(gè)兒子節(jié)點(diǎn),有兩個(gè)兒子節(jié)點(diǎn); 4.AVL樹,在二叉查找樹基礎(chǔ)上加平衡條件,旋轉(zhuǎn)方法,單旋轉(zhuǎn),雙旋轉(zhuǎn);

    2024年02月10日
    瀏覽(21)
  • LeetCode 1019. Next Greater Node In Linked List【單調(diào)棧模板】中等

    本文屬于「征服LeetCode」系列文章之一,這一系列正式開始于2021/08/12。由于LeetCode上部分題目有鎖,本系列將至少持續(xù)到刷完所有無鎖題之日為止;由于LeetCode還在不斷地創(chuàng)建新題,本系列的終止日期可能是永遠(yuǎn)。在這一系列刷題文章中,我不僅會講解多種解題思路及其優(yōu)化,

    2023年04月18日
    瀏覽(16)
  • 二叉搜索樹(Binary Search Tree,BST)

    二叉搜索樹(Binary Search Tree,BST)

    二叉搜索樹(Binary Search Tree),也稱二叉查找樹或二叉排序樹,是一種特殊的二叉樹,它滿足以下性質(zhì) 對于二叉搜索樹的每個(gè)節(jié)點(diǎn) 左子樹中的所有節(jié)點(diǎn)的值都小于該節(jié)點(diǎn)的值 右子樹中的所有節(jié)點(diǎn)的值都大于(或等于)該節(jié)點(diǎn)的值 對于二叉搜索樹的任意節(jié)點(diǎn),其左子樹和右子

    2024年02月09日
    瀏覽(21)
  • 二叉查找樹(binary search tree)(難度7)

    二叉查找樹(binary search tree)(難度7)

    C++數(shù)據(jù)結(jié)構(gòu)與算法實(shí)現(xiàn)(目錄) 答案在此:二叉查找樹(binary search tree)(答案) 寫在前面 部分內(nèi)容參《算法導(dǎo)論》 基本接口實(shí)現(xiàn) 1 刪除 刪除值為value的第一個(gè)節(jié)點(diǎn) 刪除葉子節(jié)點(diǎn)1 刪除葉子節(jié)點(diǎn)1 (3)刪除葉子節(jié)點(diǎn)1 刪除有兩個(gè)孩子的節(jié)點(diǎn)z 分成下面幾個(gè)步驟進(jìn)行: 1 找到

    2024年02月10日
    瀏覽(13)
  • 二叉搜索樹(Binary_Search_Tree)

    二叉搜索樹(Binary_Search_Tree)

    二叉搜索樹又稱二叉排序樹,它或者是一棵空樹,或者是具有以下性質(zhì)的二叉樹: 若它的左子樹不為空,則左子樹上所有節(jié)點(diǎn)的值都小于根節(jié)點(diǎn)的值。 若它的右子樹不為空,則右子樹上所有節(jié)點(diǎn)的值都大于根節(jié)點(diǎn)的值。 它的左右子樹也分別為二叉搜索樹。 如: a、從根開始

    2024年04月28日
    瀏覽(20)
  • 【C++】二叉搜索樹Binary Search Tree

    【C++】二叉搜索樹Binary Search Tree

    二叉搜索樹又被稱為二叉排序樹,顧名思義,當(dāng)我們使用中序遍歷時(shí),會得到一個(gè)有序的序列。二叉搜索樹有如下的性質(zhì): 1.若它的左子樹不為空,則左子樹上每個(gè)節(jié)點(diǎn)的值都小于根節(jié)點(diǎn)的值。 2.若它的右子樹不為空,則右子樹上每個(gè)節(jié)點(diǎn)的值都大于根節(jié)點(diǎn)的值。 3.左右子樹

    2024年02月07日
    瀏覽(29)
  • 最優(yōu)二叉搜索樹(Optimal Binary Search Tree)_20230401

    最優(yōu)二叉搜索樹(Optimal Binary Search Tree)_20230401

    前言 如果有序數(shù)組或有序表中的各個(gè)元素查找概率相等,那么采用二叉搜索樹(BST)進(jìn)行折半查找,性能最優(yōu)。如果有序表中各個(gè)記錄的查找概率不相等,情況又如何呢? 先看一個(gè)具體例子。已知有序表keys, 同時(shí)給出各個(gè)元素的查詢頻率,注意到各個(gè)元素的查詢頻率不相同。

    2024年02月12日
    瀏覽(41)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包