Approach: Check if left of root is mirror of right of root.
public bool IsSymmetric()
{
if (this.Root == null)
{
return true;
}
return this.AreMirrorInternal(this.Root.LeftNode, this.Root.RightNode);
}
private bool AreMirrorInternal(BinaryTreeNode p, BinaryTreeNode q)
{
if (p == null)
{
return q == null;
}
else if (q == null)
{
return false;
}
return p.Value == q.Value && this.AreMirrorInternal(p.LeftNode, q.RightNode) && this.AreMirrorInternal(p.RightNode, q.LeftNode);
}
No comments:
Post a Comment