忘了在哪里看到的题目了,有这样一种树形结构(如下图),用递归的方法查找是否存在某个节点。
网上找了很久,都是遍历子节点的,都不是我想要的答案,于是自己手动写了,想知道各位大佬还有没有其他方法。
上代码:
package com.founder.ipc.reconciliation.controller;
import java.util.ArrayList;
import java.util.List;
/**
* 递归查找是否存在某个树节点
* @author
* @date
*/
public class Test {
public static void main(String[] args) {
Test t = new Test();
TreeNode root = t.treeInit();
TreeNode tn = t.findById(root, "2-1");
System.out.println(tn == null ? "未找到" : "找到了~~~");
}
/**
* 递归查找某个节点
**/
private TreeNode findById(TreeNode treeNode, String id) {
i