0
public class Solution {
    public TreeNode ReverseOddLevels(TreeNode root) {
        
        root = reverse(root, false);

        return root;
        
    }

    public TreeNode reverse(TreeNode root, bool isOdd){
        if(root == null)
            return root;
        
        if(isOdd)
            isOdd = false;
        else
            isOdd = true;
        
        TreeNode right = reverse(root.right, isOdd);
        TreeNode left = reverse(root.left, isOdd);

        if(isOdd){
            root.right.val = left.val;
            root.left.val = right.val;
        }
        return root;

    }
}

I don't understand why this error occurs Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.

0 Answers0