Cousins in Binary Tree LeetCode solution in JavaScript
1var isCousins = function (root, x, y) {2let xHeight = 1;3let xParent = null;4let yHeight = 1;5let yParent = null;67const helper = (node, depth, parent) => {8if (node === null) return null;910helper(node.left, depth + 1, node);11helper(node.right, depth + 1, node);1213let val = node.val;1415if (val === x) {16xHeight = depth;17xParent = parent;18}1920if (val === y) {21yHeight = depth;22yParent = parent;23}24};2526helper(root, 0);2728return xHeight === yHeight && xParent !== yParent ? true : false;29};