C#(sharp) recursive solution, complexity O(n)


  • 0
        public bool IsSymmetric(TreeNode root) {
            if (root == null) return true;
            return IsMirror(root.left, root.right);
        }
    
        public static bool IsMirror(TreeNode left, TreeNode right)
        {
                if (left == null && right == null) return true;
                if (left == null || right == null) return false;
                if (left.val == right.val) return IsMirror(left.left, right.right) && IsMirror(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.