用递归解决树的问题(Solve Tree Problems Recursively)

Recursion is one of the most powerful and frequently used techniques for solving tree problems.

解决树的问题,递归是最有效也是使用最多的技术。

Typically, we can solve a tree problem recursively using a top-down approach or using a bottom-up approach.

典型地,我们能使用“top-down”从顶到底的方式或者“bottom-up”从底到顶的方式递归解决树的问题。

It is not easy to understand recursion and find out a recursive solution for the problem. It needs practice.

 

When you meet a tree problem, ask yourself two questions: Can you determine some parameters to help the node know its answer? Can you use these parameters and the value of the node itself to determine what should be the parameters passed to its children? If the answers are both yes, try to solve this problem using a "top-down" recursive solution.

当你遇到一个树问题时,问你自己两个问题:你能得到一些参数能帮助node知道它自己的结果吗?你能使用这些参数和node的结果去得到什么样的参数被传递给children node吗?如果两个回答都是肯定的,那么试着去解决这个问题,采用“top-down”递归方案。

Or, you can think of the problem in this way: for a node in a tree, if you know the answer of its children, can you calculate the answer of that node? If the answer is yes, solving the problem recursively using a bottom up approach might be a good idea.

如果你知道children node的结果,就能计算出父node的结果。那么可以采用“bottom-up”的方案。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用递归解决形结构数据循环调用问题的关键是要踪已经访问过的节点,以免进入无限循环。下是一个示例,演示了如何使用递归解决形结构数据循环调用问题: ```javascript // 节点类 class TreeNode { constructor(value) { this.value = value; this.children = []; } addChild(childNode) { this.children.push(childNode); } } // 递归遍历 function traverseTree(node, visitedNodes = new Set()) { // 如果节点已经被访问过,则直接返回 if (visitedNodes.has(node)) { return; } // 访问当前节点 console.log(node.value); visitedNodes.add(node); // 遍历子节点 for (const child of node.children) { traverseTree(child, visitedNodes); } } // 创建形结构数据 const root = new TreeNode('A'); const nodeB = new TreeNode('B'); const nodeC = new TreeNode('C'); const nodeD = new TreeNode('D'); const nodeE = new TreeNode('E'); root.addChild(nodeB); root.addChild(nodeC); nodeB.addChild(nodeD); nodeD.addChild(nodeE); // 循环调用测试 nodeE.addChild(root); // 在 E 节点中添加对根节点的引用 // 使用递归遍历 const visitedNodes = new Set(); traverseTree(root, visitedNodes); ``` 在这个示例中,我们定义了一个 `TreeNode` 类表示的节点,每个节点都有一个值属性 `value` 和一个子节点数组 `children`。我们使用 `addChild` 方法添加子节点。 `traverseTree` 函数用于递归遍历,并通过一个 `Set` 数据结构来记录已经访问过的节点,以防止无限循环。在访问节点时,我们打印节点的值,并将节点添加到已访问节点集合中。 在创建形结构数据后,我们对节点 `E` 执行了一个循环调用测试,将根节点 `A` 作为子节点添加到了 `E` 节点。在使用递归遍历时,我们传入一个空的已访问节点集合,并在遍历过程中传递该集合,确保只访问未被访问过的节点,避免了无限循环。 请注意,以上示例仅提供了一个基本的使用递归解决形结构数据循环调用问题的思路。在实际应用中,可能需要根据具体情况进行适当的调整和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值