我正在研究打印二叉树的所有路径的问题,这会产生结果。我创建了一个全局变量 sw ,并且在 printAllRootToLeafPaths 方法的递归中创建了一个String变量正在使用路径 。我是否可以通过某种方式在 printAllRootToLeafPaths >> printAllRootToLeafPaths < printAllRootToLeafPaths < em> 方法?因此,该方法将如下所示。
public static ArrayList<String> printAllRootToLeafPaths(TreeNode node ){
/*
String path and ArrayList<String> sw will be initiated here
*/
}
==============================================================================
import java.io.*;
import java.util.*;
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class myTest {
public static ArrayList<String> sw = new ArrayList<String>();
public static void main ( String[] args ){
TreeNode root = new TreeNode( 1 );
root.left= new TreeNode( 2 ) ;
root.left.left = new TreeNode( 5 );
root.right = new TreeNode(3);
sw = printAllRootToLeafPaths( root, new String() );
String[] result = new String[ sw.size() ];
int count = 0 ;
for ( String s: sw ){
result[count] = '"'+ s + '"';
count++;
}
System.out.println( Arrays.toString( result ) );
}
public static ArrayList<String> printAllRootToLeafPaths(TreeNode node, String path ) {
if( node==null ) return null ;
path += String.valueOf(node.val)+ "->";
if( node.left == null && node.right == null ){
String my = path.substring(0, path.length() -2 );
sw.add( my );
// optional
path = "";
}
else {
printAllRootToLeafPaths( node.left, new String (path) );
printAllRootToLeafPaths( node.right, new String (path) );
}
return sw ;
}
}
答案 0 :(得分:0)
sw = printAllRootToLeafPaths(node.left, new String());
sw = printAllRootToLeafPaths(node.right, new String());
当你传递路径时(而不是new String()
是你在所有方法调用中使用单个对象,这意味着,当你返回到原始调用者时,该对象与它的状态不同然后。调用递归方法
答案 1 :(得分:0)
看看这个解决方案:
switch
如您所见,您只需要printAllRootToLeafPaths中的路径字符串。但是,这个函数有两个参数。您可以替换全局变量的第二个参数,但是使用递归会更难维护。
此代码的结果是:
class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode(int x) {
val = x;
}
}
public class myTest {
public static void main(String[] args) {
TreeNode root = new TreeNode(1);
root.left = new TreeNode(2);
root.left.left = new TreeNode(5);
root.right = new TreeNode(3);
printAllRootToLeafPaths(root, new String());
}
public static void printAllRootToLeafPaths(TreeNode node, String path) {
path = path + " -> " + node.val;
if (isLeaf(node)) {
System.out.println(path);
} else if (node.left == null && node.right != null) {
printAllRootToLeafPaths(node.right, path);
} else if (node.left != null && node.right == null) {
printAllRootToLeafPaths(node.left, path);
} else {
printAllRootToLeafPaths(node.left, path);
printAllRootToLeafPaths(node.right, path);
}
}
public static boolean isLeaf(TreeNode t) {
if (t.left == null && t.right == null) {
return true;
}
return false;
}
}
答案 2 :(得分:0)
public static List<String> printAllRootToLeafPaths(TreeNode node ){
/*
String path and ArrayList<String> sw will be initiated here
*/
List<String> sw =new ArrayList<String>();
StringBuilder path=new StringBuilder();
printAllRootToLeafPaths(TreeNode node,path,sw ) ;
}
如果函数不是静态的,则函数需要将引用传递给每个调用中的列表
public static List<String> printAllRootToLeafPaths(TreeNode node, StringBuilder path,List<String> pathList )
{
...
else {
printAllRootToLeafPaths( node.left, new StringBuilder (path.toString()),pathList );
printAllRootToLeafPaths( node.right, new StringBuilder (path.toString()),pathList );
}