Tree Symmetric#10115

Tree Symmetric

1 secondo
128 megabyte
Facile

Given a binary tree. Check whether it is a mirror of itself (i.e. symmetric around its center).

Definition of a tree:

// Java
class TreeNode
{
public:
  int val;
  TreeNode left;
  TreeNode right;
  TreeNode(int x) {
    val = x;
    left = NULL; 
    right = NULL;
};
// C++
class TreeNode
{
public:
  int val;
  TreeNode *left;
  TreeNode *right;
  TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

Implement function isSymmetric that returns true if tree is symmetric and false otherwise.

// Java
boolean isSymmetric(TreeNode tree)
// C++
bool isSymmetric(TreeNode *tree)

Esempi

prb10115.gif

Function isSymmetric returns true for the first tree and false for the second one.