# Algorithm: Symmetric Tree

Posted by
``````/**
* Definition for a binary tree node.
**/

class TreeNode
{
constructor(val, left, right)
{
this.val = val
this.left = left
this.right = right
}
}

/**
* @param {TreeNode} root
* @return {boolean}
**/
let isSymetric = (root) =>
{
let listOfNodes = [ root ]

return isListSymetric(listOfNodes)
}

let isListSymetric = (listOfNodes, symetric = true) =>
{
let listLength = listOfNodes.length

for(let i = 0; i < listLength; i++)
{
let currentNode = listOfNodes[0]

if (currentNode.left !== undefined)
listOfNodes.push(currentNode.left)

if (currentNode.right !== undefined)
listOfNodes.push(currentNode.right)

listOfNodes.shift()
}

if (listOfNodes.length % 2 !== 0)
return false

for (let j = 0; j < listOfNodes.length / 2; j++)
if (listOfNodes[j].val !== listOfNodes[listOfNodes.length - 1 - j].val)
return false;

if (listOfNodes.length > 0 && symetric == true)
symetric = isListSymetric(listOfNodes, symetric)

return symetric;
}

// left side
let tree4 = new TreeNode(3)
let tree5 = new TreeNode(4)
let tree2 = new TreeNode(2, tree4, tree5)

// right side
let tree6 = new TreeNode(3)
let tree7 = new TreeNode(4)
let tree3 = new TreeNode(2, tree7, tree6)
let tree1 = new TreeNode(1, tree2, tree3)``````