algorithm - Re : Given a binary search tree and a number, 找到一个路径,其节点的数据添加到给定的数字

标签 algorithm binary-tree binary-search binary-search-tree

我看到了这个问题:Given a binary search tree and a number, find a path whose node's data added to be the given number. .

它说给定一棵二叉搜索树和一个数,查找是否存在一条从根到叶的路径,使得路径上的所有数相加等于给定数。

该线程上的每个人似乎都知道执行此操作的递归方法。

我错过了什么吗?你如何递归地解决这个问题?你必须暴力破解整棵树吗?

有人可以给出一个关于如何做到这一点的大纲(粗略的想法)吗?

最佳答案

递归方法类似于f(node, len),当你通过左节点或右节点时,你将其更改为f(node->left, len-left) f(node->right,len-right)。当您进入叶节点时,您会检查当前 len==0 是否已完成。

这种递归方法实际上与蛮力相同,但是,您可以使用内存技术使其更快,就像我在那篇文章中所说的那样。

关于algorithm - Re : Given a binary search tree and a number, 找到一个路径,其节点的数据添加到给定的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8660196/

相关文章:

javascript - 缓慢的二叉搜索树插入

c++ - vector 错误,一个非常困惑的分割错误?

algorithm - 寻找间隔

将二叉树转换为其镜像树的 C 函数

algorithm - 给定一个已排序的数字数组,如何找到小于 x 的数字的大小

c++ - C++ 中 strstr() 函数的时间复杂度、空间复杂度和算法是什么?

c - 如何根据以下标准进行排序?

algorithm - 如何合并两个有限状态自动机?

c - C中的这种排序算法有什么问题?

c - 根据二叉树中的值查看两个节点是否是表兄弟