When I use jave, why the return type should be TreeNode


  • 0
    T

    I am new to java. I have a question. Why the return type must be TreeNode;
    I think even void can solve this problem, it is meaning less to use TreeNode, thanks
    public class Solution {
    public void invertTree(TreeNode root) {
    if(root==null)
    return;
    TreeNode temp;
    temp=root.left;
    root.left=root.right;
    root.right=temp;
    invertTree(root.left);
    invertTree(root.right);

    }
    

    }


  • 0

    @tjuli Indeed, you are right about this. But this will enable more choices for the users to solve this problem apart from in-place solutions.

    Besides,

    Would you please format your code properly using ``` next time?

    thanks in advance!


Log in to reply
 

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