2
* Definition for a binary tree node.3
* function TreeNode(val, left, right) {4
* this.val = (val===undefined ? 0 : val)5
* this.left = (left===undefined ? null : left)6
* this.right = (right===undefined ? null : right)10
* @param {TreeNode} root111
* @param {TreeNode} root215
var leafSimilar = function (root1, root2) {18
let leaf1 = getLeaf(root1, array1),19
leaf2 = getLeaf(root2, array2);21
if (leaf1.length !== leaf2.length) {22
// if different lengths, return false right away26
for (let i = 0; i < leaf1.length; i++) {27
if (leaf1[i] !== leaf2[i]) {28
// compare pair by pair36
var getLeaf = function (root, array) {41
if (!(root.left || root.right)) {42
array.push(root.val); // push leaf value to the array44
getLeaf(root.left, array);45
getLeaf(root.right, array);