樹Tree形結構,獲取所有葉子節點路徑

public static void main(String[] args) {
		List<String> path = new ArrayList<String>();
		List<List<String>> allPath = new ArrayList<>();
		path.add(node.name);
		fun(node, path, allPath);
	}	
public static void fun(TreeNode node, List<String> path, List<List<String>> allPah) {
		if (node.children.size() == 0) {
			allPah.add(path);
			return;
		}
		for (TreeNode cNode : node.children) {
			List<String> cPath = new ArrayList<String>();
			cPath.addAll(path);
			cPath.add(cNode.name);
			fun(cNode, cPath, allPah);
		}
	}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章