1367:二叉树中的列表
This commit is contained in:
parent
9da53e6071
commit
7fcd254111
110
src/main/java/leetcode/editor/cn/LinkedListInBinaryTree.java
Normal file
110
src/main/java/leetcode/editor/cn/LinkedListInBinaryTree.java
Normal 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)
|
||||
|
||||
}
|
42
src/main/java/leetcode/editor/cn/LinkedListInBinaryTree.md
Normal file
42
src/main/java/leetcode/editor/cn/LinkedListInBinaryTree.md
Normal file
@ -0,0 +1,42 @@
|
||||
<p>给你一棵以 <code>root</code> 为根的二叉树和一个 <code>head</code> 为第一个节点的链表。</p>
|
||||
|
||||
<p>如果在二叉树中,存在一条一直向下的路径,且每个点的数值恰好一一对应以 <code>head</code> 为首的链表中每个节点的值,那么请你返回 <code>True</code> ,否则返回 <code>False</code> 。</p>
|
||||
|
||||
<p>一直向下的路径的意思是:从树中某个节点开始,一直连续向下的路径。</p>
|
||||
|
||||
<p> </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> </p>
|
||||
|
||||
<p><strong>提示:</strong></p>
|
||||
|
||||
<ul>
|
||||
<li>二叉树和链表中的每个节点的值都满足 <code>1 <= node.val <= 100</code> 。</li>
|
||||
<li>链表包含的节点数目在 <code>1</code> 到 <code>100</code> 之间。</li>
|
||||
<li>二叉树包含的节点数目在 <code>1</code> 到 <code>2500</code> 之间。</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>
|
Loading…
Reference in New Issue
Block a user