zoukankan      html  css  js  c++  java
  • [LeetCode] 1367. Linked List in Binary Tree

    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:

    • 1 <= node.val <= 100 for each node in the linked list and binary tree.
    • The given linked list will contain between 1 and 100 nodes.
    • The given binary tree will contain between 1 and 2500 nodes.

    二叉树中的列表。

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

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

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

    来源:力扣(LeetCode)
    链接:https://leetcode-cn.com/problems/linked-list-in-binary-tree
    著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。

    题意是找二叉树中是否有那么一段,节点值跟给出的单链表的节点值一一对应。这个要找的二叉树中的一段可以不是从根节点开始,但是需要符合从上往下的顺序。

    我给出一个前序遍历 + 递归的思路。首先因为单链表的头结点压根不知道在哪,所以我用递归的方式去找单链表的头结点。同时我用一个helper函数去traverse这棵树,当我从某一个节点继续往左子树/右子树比较节点的时候,我检查一下当前的左孩子/右孩子是否跟head.next.val相同。

    时间O(n)

    空间O(n)

    Java实现

     1 /**
     2  * Definition for singly-linked list.
     3  * public class ListNode {
     4  *     int val;
     5  *     ListNode next;
     6  *     ListNode() {}
     7  *     ListNode(int val) { this.val = val; }
     8  *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
     9  * }
    10  */
    11 /**
    12  * Definition for a binary tree node.
    13  * public class TreeNode {
    14  *     int val;
    15  *     TreeNode left;
    16  *     TreeNode right;
    17  *     TreeNode() {}
    18  *     TreeNode(int val) { this.val = val; }
    19  *     TreeNode(int val, TreeNode left, TreeNode right) {
    20  *         this.val = val;
    21  *         this.left = left;
    22  *         this.right = right;
    23  *     }
    24  * }
    25  */
    26 class Solution {
    27     public boolean isSubPath(ListNode head, TreeNode root) {
    28         // corner case
    29         if (head == null) {
    30             return true;
    31         }
    32         if (root == null) {
    33             return false;
    34         }
    35         // 先判断当前的节点,如果不对,再看左子树和右子树
    36         return helper(root, head) || isSubPath(head, root.left) || isSubPath(head, root.right);
    37     }
    38 
    39     private boolean helper(TreeNode root, ListNode head) {
    40         if (head == null) {
    41             return true;
    42         }
    43         if (root == null) {
    44             return false;
    45         }
    46         if (root.val != head.val) {
    47             return false;
    48         }
    49         return helper(root.left, head.next) || helper(root.right, head.next);
    50     }
    51 }

    LeetCode 题目总结

  • 相关阅读:
    CSS切割
    一台电脑 多个 tomcat
    CGI
    电源关系
    Monkey Test 命令使用
    html ul
    java 反射
    RTMP
    动态库
    flash 大文件上传
  • 原文地址:https://www.cnblogs.com/cnoodle/p/14022620.html
Copyright © 2011-2022 走看看