1367:二叉树中的列表

This commit is contained in:
huangge1199 2021-06-07 11:23:58 +08:00
parent 9da53e6071
commit 7fcd254111
2 changed files with 152 additions and 0 deletions

View File

@ -0,0 +1,110 @@
//给你一棵以 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,nu
//ll,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 之间
//
// Related Topics 链表 动态规划
// 👍 89 👎 0
package leetcode.editor.cn;
import com.code.leet.entiy.ListNode;
import com.code.leet.entiy.TreeNode;
//1367:二叉树中的列表
public class LinkedListInBinaryTree{
public static void main(String[] args) {
//测试代码
Solution solution = new LinkedListInBinaryTree().new Solution();
}
//力扣代码
//leetcode submit region begin(Prohibit modification and deletion)
/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
class Solution {
public boolean isSubPath(ListNode head, TreeNode root) {
if (root == null) {
return false;
}
return isSubPath(head, root.left) || isSubPath(head, root.right) || dfs(head, root);
}
private boolean dfs(ListNode head, TreeNode root) {
if (head == null) {
return true;
}
if (root == null) {
return false;
}
if (root.val != head.val) {
return false;
}
return dfs(head.next, root.left) || dfs(head.next, root.right);
}
}
//leetcode submit region end(Prohibit modification and deletion)
}

View File

@ -0,0 +1,42 @@
<p>给你一棵以&nbsp;<code>root</code>&nbsp;为根的二叉树和一个&nbsp;<code>head</code>&nbsp;为第一个节点的链表。</p>
<p>如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以&nbsp;<code>head</code>&nbsp;为首的链表中每个节点的值,那么请你返回 <code>True</code> ,否则返回 <code>False</code></p>
<p>一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径。</p>
<p>&nbsp;</p>
<p><strong>示例 1</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/sample_1_1720.png" style="height: 280px; width: 220px;"></strong></p>
<pre><strong>输入:</strong>head = [4,2,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>输出:</strong>true
<strong>解释:</strong>树中蓝色的节点构成了与链表对应的子路径。
</pre>
<p><strong>示例 2</strong></p>
<p><strong><img alt="" src="https://assets.leetcode-cn.com/aliyun-lc-upload/uploads/2020/02/29/sample_2_1720.png" style="height: 280px; width: 220px;"></strong></p>
<pre><strong>输入:</strong>head = [1,4,2,6], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>输出:</strong>true
</pre>
<p><strong>示例 3</strong></p>
<pre><strong>输入:</strong>head = [1,4,2,6,8], root = [1,4,4,null,2,2,null,1,null,6,8,null,null,null,null,1,3]
<strong>输出:</strong>false
<strong>解释:</strong>二叉树中不存在一一对应链表的路径。
</pre>
<p>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li>二叉树和链表中的每个节点的值都满足&nbsp;<code>1 &lt;= node.val&nbsp;&lt;= 100</code>&nbsp;</li>
<li>链表包含的节点数目在&nbsp;<code>1</code>&nbsp;&nbsp;<code>100</code>&nbsp;之间。</li>
<li>二叉树包含的节点数目在&nbsp;<code>1</code>&nbsp;&nbsp;<code>2500</code>&nbsp;之间。</li>
</ul>
<div><div>Related Topics</div><div><li></li><li>链表</li><li>动态规划</li></div></div>\n<div><li>👍 89</li><li>👎 0</li></div>