最短根到叶路径

发布于 2024-07-06 21:37:25 字数 78 浏览 16 评论 0原文

在 BST(二叉搜索树)中查找最短根到叶路径的最简单方法是什么(最好使用递归)。 首选Java,伪代码也可以。

谢谢!

What is the easiest way, preferably using recursion, to find the shortest root-to-leaf path in a BST (Binary Search Tree). Java prefered, pseudocode okay.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

呆萌少年 2024-07-13 21:37:25

一般描述:

使用广度优先搜索 (BFS)深度优先搜索 (DFS) 相反。 找到第一个没有子节点的节点。

使用 DFS,您可能会在某些输入树上幸运(但无法知道您是否幸运,因此您仍然需要搜索整个树),但使用 BFS 方法要快得多,并且您可以找到解决方案,而无需触及所有树节点。

要查找从根到叶的路径,您可以使用父引用沿着第一个找到的无子节点一直回到根。 如果每个节点中没有存储父引用,则可以在向下递归时跟踪父节点。 如果您的列表顺序相反,您可以将其全部压入堆栈,然后将其弹出。

伪代码:

问题很简单; 这是找到最小长度的伪代码:

  1. 将根节点放入队列中。

当队列不为空且未找到结果时重复:

  1. 从队列开头拉出一个节点并检查它是否没有子节点。 如果它没有孩子你
    完成后,您找到了最短路径。
  2. 否则将所有子级(左、右)推入队列。

查找所有最短路径:

要查找所有最短路径,您可以将节点的深度以及队列内的节点存储起来。 然后,您将对队列中具有相同深度的所有节点继续该算法。

替代方案:

如果您决定使用 DFS,则必须搜索整个树才能找到最短路径。 但这可以通过保留迄今为止最短的值并仅检查未来节点的深度直到找到新的最短值或直到达到迄今为止的最短值来优化。 不过,BFS 是一个更好的解决方案。

General description:

Use a Breadth-first search (BFS) as opposed to a Depth-first search (DFS). Find the first node with no children.

Using a DFS you might get lucky on some input trees (but there is no way to know you got lucky so you still need to search the whole tree), but using the BFS method is much faster and you can find a solution without touching all nodes.

To find the root to leaf path, you could follow the first found childless node all the way back up to the root using the parent reference. If you have no parent reference stored in each node, you can keep track of the parent nodes as you recurse down. If you have your list in reverse order you could push it all on a stack and then pop it off.

Pseudo-code:

The problem is very simple; here is pseudo code to find the smallest length:

  1. Put the root node on the queue.

Repeat while the queue is not empty, and no result was found:

  1. Pull a node from the beginning of the queue and check if it has no children. If it has no children you
    are done you found the shortest path.
  2. Otherwise push all the children (left, right) onto the queue.

Finding all shortest paths:

To find all shortest paths you can store the depth of the node along with node inside the queue. Then you would continue the algorithm for all nodes in the queue with the same depth.

Alternative:

If instead you decided to use a DFS, you would have to search the entire tree to find the shortest path. But this could be optimized by keeping a value for the shortest so far, and only checking the depth of future nodes up until you find a new shortest, or until you reach the shortest so far. The BFS is a much better solution though.

对你而言 2024-07-13 21:37:25

这是用 C++ 编写的,但它非常简单,您可以轻松转换它。 只需将 min 更改为 max 即可获得最大树深度。

int TreeDepth(Node* p)
{
    return (p == NULL) ? 0 : min(TreeDepth(p->LeftChild), TreeDepth(p->RightChild)) + 1;
}

只是为了解释一下它在做什么,它从叶节点开始计数(当找到叶节点时返回 0),然后向上计数到根。 对树的左侧和右侧执行此操作并取最小值将为您提供最短路径。

This is in C++, but it is so simple you can convert it easily. Just change min to max to get the maximum tree depth.

int TreeDepth(Node* p)
{
    return (p == NULL) ? 0 : min(TreeDepth(p->LeftChild), TreeDepth(p->RightChild)) + 1;
}

Just to explain what this is doing, it is counting from the leaf node (it returns 0 when it finds a leaf) and counts up back to the root. Doing this for the left and right hand sides of the tree and taking the minimum will give you the shortest path.

本宫微胖 2024-07-13 21:37:25

就访问的顶点数量而言,广度优先搜索完全是最优的。 您必须访问在广度优先搜索中访问的每个顶点,只是为了证明您拥有最近的叶子!

但是,如果您必须使用递归,那么 Mike Thompson 的方法几乎是正确的选择,而且稍微简单一些。

TD(p) is
  0 if p is NULL (empty tree special case)
  1 if p is a leaf (p->left == NULL and p->right == NULL)
  min(TD(p->left), TD(p->right)) if p is not a leaf 

Breadth first search is exactly optimal in terms of the number of vertices visited. You have to visit every one of the vertices you'd visit in a breadth first search just in order to prove you have the closest leaf!

However, if you have a mandate to use recursion, Mike Thompson's approach is almost the right one to use -- and is slightly simpler.

TD(p) is
  0 if p is NULL (empty tree special case)
  1 if p is a leaf (p->left == NULL and p->right == NULL)
  min(TD(p->left), TD(p->right)) if p is not a leaf 
愿得七秒忆 2024-07-13 21:37:25

static int findCheapestPathSimple(TreeNode root){

if(root==null){
    return 0; 
}

return root.data+Math.min(findCheapestPathSimple(root.left), findCheapestPathSimple(root.right));

}

static int findCheapestPathSimple(TreeNode root){

if(root==null){
    return 0; 
}

return root.data+Math.min(findCheapestPathSimple(root.left), findCheapestPathSimple(root.right));

}

心如狂蝶 2024-07-13 21:37:25
shortestPath(X)
if X == NIL
    return 0
else if X.left == NIL and X.right == NIL //X is a leaf
    return 1
else
    if X.left == NIL
        return 1 + shortestPath(X.right)
    else if X.right == NIL
        return 1 + shortestPath(X.left)
    else
        return 1 + min(shortestPath(X.left), shortestPath(X.right))
shortestPath(X)
if X == NIL
    return 0
else if X.left == NIL and X.right == NIL //X is a leaf
    return 1
else
    if X.left == NIL
        return 1 + shortestPath(X.right)
    else if X.right == NIL
        return 1 + shortestPath(X.left)
    else
        return 1 + min(shortestPath(X.left), shortestPath(X.right))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文