# Symmetrical Tree

![](/files/T54bqzDcokX16xHlbCtG)

* n, h

```jsx
// This is an input class. Do not edit.
class BinaryTree {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

exports.BinaryTree = BinaryTree;

function symmetricalTree(tree) {
  
  return validate(tree.left, tree.right);
}

function validate(left, right) {
  if (left === null && right === null) {
    return true;
  }

  if (left === null || right === null || left.value !== right.value) {
    return false;
  }

  const outer = validate(left.left, right.right);
  const inner = validate(left.right, right.left);

  return outer && inner;
}

// Do not edit the line below.
exports.symmetricalTree = symmetricalTree;
```

* iteration

```jsx
// This is an input class. Do not edit.
class BinaryTree {
  constructor(value) {
    this.value = value;
    this.left = null;
    this.right = null;
  }
}

exports.BinaryTree = BinaryTree;

function symmetricalTree(tree) {

  const leftStack = [tree.left];
  const rightStack = [tree.right];

  while (leftStack.length > 0) {
    const left = leftStack.pop();
    const right = rightStack.pop();

    if (left === null && right === null) continue;
    if (left === null || right === null || left.value !== right.value) return false;

    leftStack.push(left.left);
    leftStack.push(left.right);
    rightStack.push(right.right);
    rightStack.push(right.left);
  }

  
  return true;
}

// Do not edit the line below.
exports.symmetricalTree = symmetricalTree;
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://algorithm.prettylog.com/algorithm-problems/algoexpert/medium/symmetrical-tree.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
