1261. Find Elements in a Contaminated Binary Tree #1342
-
Topics: Given a binary tree with the following rules:
Now the binary tree is contaminated, which means all Implement the
Example 1:
Example 2:
Example 3:
Constraints:
Hint:
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
We need to recover the values of a contaminated binary tree and efficiently check if a target value exists in the recovered tree. The tree's structure is preserved, but all node values are set to -1. The original tree follows specific rules where each node's value determines its children's values. We need to recover these values and provide an efficient way to check for the existence of a target value. Approach
Let's implement this solution in PHP: 1261. Find Elements in a Contaminated Binary Tree <?php
/**
* 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 FindElements {
private $values;
/**
* @param TreeNode $root
*/
function __construct($root) {
$this->values = array();
if ($root === null) {
return;
}
$queue = new SplQueue();
$root->val = 0;
$this->values[$root->val] = true;
$queue->enqueue($root);
while (!$queue->isEmpty()) {
$node = $queue->dequeue();
$x = $node->val;
if ($node->left !== null) {
$leftVal = 2 * $x + 1;
$node->left->val = $leftVal;
$this->values[$leftVal] = true;
$queue->enqueue($node->left);
}
if ($node->right !== null) {
$rightVal = 2 * $x + 2;
$node->right->val = $rightVal;
$this->values[$rightVal] = true;
$queue->enqueue($node->right);
}
}
}
/**
* @param Integer $target
* @return Boolean
*/
function find($target) {
return isset($this->values[$target]);
}
}
// ========== USAGE EXAMPLES ==========
// Example 1:
// Input: ["FindElements","find","find"]
// [[[-1,null,-1]],[1],[2]]
// Output: [null, false, true]
$root1 = new TreeNode(-1);
$root1->right = new TreeNode(-1);
$findElements1 = new FindElements($root1);
echo "Example 1:\n";
echo $findElements1->find(1) ? 'true' : 'false'; // Output: false
echo "\n";
echo $findElements1->find(2) ? 'true' : 'false'; // Output: true
echo "\n\n";
// Example 2:
// Input: ["FindElements","find","find","find"]
// [[[-1,-1,-1,-1,-1]],[1],[3],[5]]
// Output: [null,true,true,false]
$root2 = new TreeNode(-1);
$root2->left = new TreeNode(-1);
$root2->right = new TreeNode(-1);
$root2->left->left = new TreeNode(-1);
$root2->left->right = new TreeNode(-1);
$findElements2 = new FindElements($root2);
echo "Example 2:\n";
echo $findElements2->find(1) ? 'true' : 'false'; // Output: true
echo "\n";
echo $findElements2->find(3) ? 'true' : 'false'; // Output: true
echo "\n";
echo $findElements2->find(5) ? 'true' : 'false'; // Output: false
echo "\n\n";
// Example 3:
// Input: ["FindElements","find","find","find","find"]
// [[[-1,null,-1,-1,null,-1]],[2],[3],[4],[5]]
// Output: [null,true,false,false,true]
$root3 = new TreeNode(-1);
$root3->right = new TreeNode(-1);
$root3->right->left = new TreeNode(-1);
$root3->right->left->right = new TreeNode(-1);
$findElements3 = new FindElements($root3);
echo "Example 3:\n";
echo $findElements3->find(2) ? 'true' : 'false'; // Output: true
echo "\n";
echo $findElements3->find(3) ? 'true' : 'false'; // Output: false
echo "\n";
echo $findElements3->find(4) ? 'true' : 'false'; // Output: false
echo "\n";
echo $findElements3->find(5) ? 'true' : 'false'; // Output: true
echo "\n";
?> Explanation:
This approach ensures that the tree is recovered correctly and efficiently, and target checks are performed in constant time, providing an optimal solution to the problem. |
Beta Was this translation helpful? Give feedback.
We need to recover the values of a contaminated binary tree and efficiently check if a target value exists in the recovered tree. The tree's structure is preserved, but all node values are set to -1. The original tree follows specific rules where each node's value determines its children's values. We need to recover these values and provide an efficient way to check for the existence of a target value.
Approach