Found the topic of the tree.
The instances created inside the function are recycled if they are only assigned to the parameter.
Function
1 Public Static void Crete (TreeNode node) {2 New TreeNode (111); 3 }
Call
1 NULL ; 2 treeutil.crete (node); 3 System.out.println (node.val);
Abnormal
1 Exception in thread "main" java.lang.NullPointerException2at Leetcode101.main ( LEETCODE101.JAVA:28)
In principle, we have created an instance of this parameter in the function.
However, this instance will be recycled at the end of the function, so it is still the original null.
What's going to happen? How can I get the result after the operation in the function?
Use the created instance as the return value!
Function
1 Public Static TreeNode Create (int x) {2 new TreeNode (x); 3 New TreeNode (x+1); 4 New TreeNode (x+2); 5 return node; 6 }
Call
1 TreeNode node = treeutil.create (1); 2 System.out.println (node.val); 3 System.out.println (node.left.val); 4 System.out.println (Node.right.val);
Output
123
Not only is node's memory still there, his left and right children are still
Memory recovery pits at the end of Java functions