LeetCode/101. SymmetricTree

Summary

Figure out given TreeNode is symmetric or not. I was thinking for a while and able to get an answer.

Solutions

My Solution

I feel like now I have some mindset that I can solve programming problem with using my brain. Using BFS to find an answer.

public class SymmetricTree
{
    public bool IsSymmetric(TreeNode root)
    {
        if (root == null) return true;

        var result = true;
        var queue = new Queue<TreeNode>();
        queue.Enqueue(root.left);
        queue.Enqueue(root.right);

        TreeNode left;
        TreeNode right;

        while (queue.Count != 0)
        {
            left = queue.Dequeue();
            right = queue.Dequeue();

            // Break the while loop
            if (left == null && right == null) continue;
            if (left != null && right == null) return false;
            if (left == null && right != null) return false;
            if (left.val != right.val) return false;

            queue.Enqueue(left.left);
            queue.Enqueue(right.right);
            queue.Enqueue(left.right);
            queue.Enqueue(right.left);
        }

        return result;
    }
}

Best Solution

My solution is very close to the best solution, except checking if part, I’ll skip.