1367. Linked List in Binary Tree #489
-
Topics: Given a binary tree Return True if all the elements in the linked list starting from the In this context downward path means a path that starts at some node and goes downwards. Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to recursively check whether a linked list can match a downward path in a binary tree. We'll use depth-first search (DFS) to explore the binary tree and attempt to match the linked list from its head to the leaf nodes. Here’s how we can approach the solution: Steps:
Let's implement this solution in PHP: 1367. Linked List in Binary Tree <?php
// Definition for a singly-linked list node.
class ListNode {
public $val = 0;
public $next = null;
function __construct($val = 0, $next = null) {
$this->val = $val;
$this->next = $next;
}
}
// Definition for a binary tree node.
class TreeNode {
public $val = 0;
public $left = null;
public $right = null;
function __construct($val = 0, $left = null, $right = null) {
$this->val = $val;
$this->left = $left;
$this->right = $right;
}
}
class Solution {
/**
* @param ListNode $head
* @param TreeNode $root
* @return Boolean
*/
function isSubPath($head, $root) {
// Base case: If root is null, no path can exist.
if ($root === null) {
return false;
}
// Check if the current node is the starting point of the linked list.
if ($this->dfs($head, $root)) {
return true;
}
// Otherwise, keep searching in the left and right subtrees.
return $this->isSubPath($head, $root->left) || $this->isSubPath($head, $root->right);
}
// Helper function to match the linked list starting from the current tree node.
function dfs($head, $root) {
// If the linked list is fully traversed, return true.
if ($head === null) {
return true;
}
// If the binary tree node is null or the values don't match, return false.
if ($root === null || $head->val !== $root->val) {
return false;
}
// Continue the search downwards in both left and right subtrees.
return $this->dfs($head->next, $root->left) || $this->dfs($head->next, $root->right);
}
}
// Example usage:
// Linked List: 4 -> 2 -> 8
$head = new ListNode(4);
$head->next = new ListNode(2);
$head->next->next = new ListNode(8);
// Binary Tree:
// 1
// / \
// 4 4
// \ \
// 2 2
// / \ / \
// 1 6 8 8
$root = new TreeNode(1);
$root->left = new TreeNode(4);
$root->right = new TreeNode(4);
$root->left->right = new TreeNode(2);
$root->right->left = new TreeNode(2);
$root->left->right->left = new TreeNode(1);
$root->left->right->right = new TreeNode(6);
$root->right->left->right = new TreeNode(8);
$root->right->left->right = new TreeNode(8);
$solution = new Solution();
$result = $solution->isSubPath($head, $root);
echo $result ? "true" : "false"; // Output: true
?> Explanation:
Example Execution:For input
Complexity:
This solution efficiently checks for the subpath in the binary tree using DFS. |
Beta Was this translation helpful? Give feedback.
We need to recursively check whether a linked list can match a downward path in a binary tree. We'll use depth-first search (DFS) to explore the binary tree and attempt to match the linked list from its head to the leaf nodes.
Here’s how we can approach the solution:
Steps:
true
if the …