Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree
Description
null
Solution(javascript)
/**
* 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 {TreeNode} root
* @param {number[]} arr
* @return {boolean}
*/
var isValidSequence = function(root, arr) {
const aux = (node, index) => {
if(!node) {
return false
}
if(node.val !== arr[index]) {
return false
}
if(index === arr.length - 1) {
if(!node.left && !node.right) {
return true
}
return false
}
console.log(node.val, arr[index])
return aux(node.left, index+1) || aux(node.right, index+1)
}
return aux(root, 0)
};