C# solution: DFS


  • 0
    B
    public class Solution 
    {
        public bool IsSymmetric(TreeNode root) 
        {
            if (root == null) return true;
    
            return IsSymmetric(root.left, root.right);
        }
    
        private bool IsSymmetric(TreeNode left, TreeNode right)
        {
            if (left == null && right == null) return true;
            if (left == null || right == null) return false;
    
            if (left.val == right.val)
            {
                return IsSymmetric(left.left, right.right) && IsSymmetric(left.right, right.left);
            }
            return false;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.