zoukankan      html  css  js  c++  java
  • LeetCode 1367 二叉树中的列表

    题目描述:

    给你一棵以 root 为根的二叉树和一个 head 为第一个节点的链表。

    如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 head 为首的链表中每个节点的值,那么请你返回 True ,否则返回 False 。

    一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径。

    示例 1:

    输入:head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
    输出:true
    解释:树中蓝色的节点构成了与链表对应的子路径。
    示例 2:

    输入:head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
    输出:true
    示例 3:

    输入:head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
    输出:false
    解释:二叉树中不存在一一对应链表的路径。
     

    提示:

    二叉树和链表中的每个节点的值都满足 1 <= node.val <= 100 。
    链表包含的节点数目在 1 到 100 之间。
    二叉树包含的节点数目在 1 到 2500 之间。

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/linked-list-in-binary-tree

    自己的错误思路:自己一开始在解答这道题时,基本思路出了问题导致最后一个用例不能通过,当时的想法是这样的,我们用代码描述一下,即递归函数有三个参数head(保存链表首个节点),root当前树

    匹配到节点,h当前链表匹配节点:如果root为空返回false,如果匹配到最后一个节点,且树节点值与链表最后一个节点值相等返回true。如果当前树节点和链表节点值不相等,链表从头开始和当前root(链表已移动到非首元节点上)或者当前左右节点匹配。如果当前树节点和链表节点值相等,则双方均向下移动。但经过实践证明此种思路是错误的,个人分析主要原因还是在于

           if(h->val!=root->val){
               if(h!=head)
                  return judge(head,root,head);//||judge(head,root,head);
                else
                  return judge(head,root->left,head)||judge(head,root->right,head);
           }
    此段代码上,当不相等时树并不会回溯到当时从头开始匹配的树的节点的子节点,导致遗漏。
    class Solution {
    public:
        bool isSubPath(ListNode* head, TreeNode* root) {
                ListNode *h;
                h=head;
                return   judge(head,root,h);
        }
        bool judge(ListNode *head,TreeNode *root,ListNode *h){
             if(root==NULL){
                return false;
            }
            if(h->next==NULL&&h->val==root->val){
                return true;
            }

           if(h->val!=root->val){
               if(h!=head)
                  return judge(head,root,head);//||judge(head,root,head);
                else
                  return judge(head,root->left,head)||judge(head,root->right,head);
           }

            if(h->val==root->val)
               return judge(head,root->left,h->next)||judge(head,root->right,h->next);
            return false;
        }
    };

    最后参考leetcode官方题解给的思路,通过枚举的方法实现,即对于每个节点判断其从该节点开始的路径是否满足条件。具体代码如下:

    class Solution {
    public:
        bool isSubPath(ListNode* head, TreeNode* root) {
              if(root==NULL){
                  return false;
              }
              return dfs(head,root)||isSubPath(head,root->left)||isSubPath(head,root->right);
    
        }
        bool dfs(ListNode *head,TreeNode *root){
            if(head==NULL)
               return true;
            if(root==NULL){
                return false;
            }
            if(head->val!=root->val){
                return false;
            }
            return dfs(head->next,root->left)||dfs(head->next,root->right);
        }
        
    };

    基本dfs函数思路为,如果head为空代表满足条件,root为空不满足,并且值不相等也不满足,如果相等,则继续递归。而主函数进行枚举,对于当前节点判断是否满足,并判断其左右子节点是否满足,如此递归。

  • 相关阅读:
    浅谈SharePoint 2013 站点模板开发 转载自http://www.cnblogs.com/jianyus/p/3511550.html
    SharePoint 入门书籍推荐 转载来源http://www.cnblogs.com/jianyus/p/3513238.html
    php简易计算器
    php的常量
    php数据类型的转换
    php的date()函数
    php流程控制语句
    php的运算符
    php简介
    手机页面操作栏的创建及WebFont的使用
  • 原文地址:https://www.cnblogs.com/zzw-/p/13280371.html
Copyright © 2011-2022 走看看