C++
Java
Python
Python3
C
C#
JavaScript
Ruby
Swift
Go
Scala
Kotlin
Rust
PHP
TypeScript
Racket
Erlang
Elixir
Dart
monokai
ambiance
chaos
chrome
cloud9_day
cloud9_night
cloud9_night_low_color
clouds
clouds_midnight
cobalt
crimson_editor
dawn
dracula
dreamweaver
eclipse
github
github_dark
gob
gruvbox
gruvbox_dark_hard
gruvbox_light_hard
idle_fingers
iplastic
katzenmilch
kr_theme
kuroir
merbivore
merbivore_soft
mono_industrial
nord_dark
one_dark
pastel_on_dark
solarized_dark
solarized_light
sqlserver
terminal
textmate
tomorrow
tomorrow_night
tomorrow_night_blue
tomorrow_night_bright
tomorrow_night_eighties
twilight
vibrant_ink
xcode
上次编辑到这里,代码来自缓存 点击恢复默认模板
/**
* 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) {
}
};
运行代码
提交
golang 解法, 执行用时: 4 ms, 内存消耗: 6.7 MB, 提交时间: 2023-10-25 23:04:34
/**
* Definition for singly-linked list.
* type ListNode struct {
* Val int
* Next *ListNode
* }
*/
/**
* Definition for a binary tree node.
* type TreeNode struct {
* Val int
* Left *TreeNode
* Right *TreeNode
* }
*/
func isSubPath(head *ListNode, root *TreeNode) bool {
if root == nil {
return false
}
return isSubPathHelper(head, root) || isSubPath(head, root.Left) || isSubPath(head, root.Right)
}
func isSubPathHelper(head *ListNode, root *TreeNode) bool {
if head == nil {
return true
}
if root == nil {
return false
}
if head.Val != root.Val {
return false
}
return isSubPathHelper(head.Next, root.Right) || isSubPathHelper(head.Next, root.Left)
}
python3 解法, 执行用时: 88 ms, 内存消耗: 18.2 MB, 提交时间: 2023-10-25 23:04:02
# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:
def dfs(self, head: ListNode, rt: TreeNode) -> bool:
if not head:
return True
if not rt:
return False
if rt.val != head.val:
return False
return self.dfs(head.next, rt.left) or self.dfs(head.next, rt.right)
def isSubPath(self, head: ListNode, root: TreeNode) -> bool:
if not root:
return False
return self.dfs(head, root) or self.isSubPath(head, root.left) or self.isSubPath(head, root.right)
javascript 解法, 执行用时: 76 ms, 内存消耗: 47.7 MB, 提交时间: 2023-10-25 23:03:38
/**
* Definition for singly-linked list.
* function ListNode(val, next) {
* this.val = (val===undefined ? 0 : val)
* this.next = (next===undefined ? null : next)
* }
*/
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {ListNode} head
* @param {TreeNode} root
* @return {boolean}
*/
var isSubPath = function(head, root) {
if (root == null) return 0;
return dfs(root, head) || isSubPath(head, root.left) || isSubPath(head, root.right);
};
var dfs = function(rt, head) {
if (head == null) return true;
if (rt == null) return false;
if (rt.val != head.val) return false;
return dfs(rt.left, head.next) || dfs(rt.right, head.next);
}
cpp 解法, 执行用时: 28 ms, 内存消耗: 31.8 MB, 提交时间: 2023-10-25 23:03:16
/**
* 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 {
bool dfs(TreeNode* rt, ListNode* head) {
// 链表已经全部匹配完,匹配成功
if (head == NULL) return true;
// 二叉树访问到了空节点,匹配失败
if (rt == NULL) return false;
// 当前匹配的二叉树上节点的值与链表节点的值不相等,匹配失败
if (rt->val != head->val) return false;
return dfs(rt->left, head->next) || dfs(rt->right, head->next);
}
public:
bool isSubPath(ListNode* head, TreeNode* root) {
if (root == NULL) return false;
return dfs(root, head) || isSubPath(head, root->left) || isSubPath(head, root->right);
}
};
cpp 解法, 执行用时: 24 ms, 内存消耗: 31.7 MB, 提交时间: 2023-10-25 23:02:49
/**
* 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 == nullptr) return true;
if(root == nullptr) return false;
return dfs(head,root) || isSubPath(head,root->left) || isSubPath(head,root->right);
}
bool dfs(ListNode *head,TreeNode *node){
if(head == nullptr) return true;
if(node == nullptr) return false;
if(head->val != node->val) return false;
return dfs(head->next,node->left) || dfs(head-> next,node->right);
}
};
java 解法, 执行用时: 1 ms, 内存消耗: 42.3 MB, 提交时间: 2023-10-25 23:02:32
/**
* 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 (head == null) {
return true;
}
if (root == null) {
return false;
}
//先判断当前的节点,如果不对,再看左子树和右子树呗
return isSub(head, root) || isSubPath(head, root.left) || isSubPath(head, root.right);
}
private boolean isSub(ListNode head, TreeNode node) {
//特判:链表走完了,返回true
if (head == null) {
return true;
}
//特判:链表没走完,树走完了,这肯定不行,返回false
if (node == null) {
return false;
}
//如果值不同,必定不是啊
if (head.val != node.val) {
return false;
}
//如果值相同,继续看,左边和右边有一个满足即可
return isSub(head.next, node.left) || isSub(head.next, node.right);
}
}