如何使用 Java 创建二叉树最大深度中包含的节点的链表

发布于 2024-08-09 16:26:54 字数 88 浏览 3 评论 0原文

我已经创建了二叉树和链接列表类,我只需要一种仅打印最大路径的节点的算法。二叉树的高度和大小已经存储在根节点中,但我的问题是在将每个节点添加到链表时仅遍历最大路径。

I've already created the Binary Tree and Linked list classes, I'm just in need of an algorithm that prints ONLY the nodes of the largest path. The height and size of the Binary Tree are already stored in the root node, but my problem is traversing only the largest path while adding each node to my linked list.

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

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

发布评论

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

评论(2

十级心震 2024-08-16 16:26:54

我假设您的二叉树节点有对其父节点的引用,对吗?然后,您可以使用广度优先搜索或深度优先搜索并查找深度等于最大深度的根节点。一旦找到一个这样的节点,然后从那里沿着父节点的轨迹向上,并将沿途的每个节点添加到链接列表中。当到达顶部时,链表就有了最大路径的所有节点。

该算法如下所示:

  1. 从二叉树的根部开始,
  2. 使用广度优先或深度优先搜索来到达叶节点(没有任何子节点的节点)
    1. 当到达叶节点时,检查它的深度:
      1. 如果不等于最大深度,则忽略它并继续搜索
      2. 如果它等于最大深度,那么您就找到了最大路径的结束节点(可能有多个,但此时区分它们似乎并不重要)。进入下一步
  3. 将此叶节点添加到链表中,然后转到其父节点,
  4. 重复最后一步,直到到达根节点并且没有父节点 - 那么您的链表就有最长路径的所有节点。

请注意,节点的顺序是从叶节点到根节点 - 如果需要反转它,可以在最后一步之后执行此操作。

I assume your binary tree nodes have a reference to their parent, is that right? Then you could use either a breadth-first search or a depth-first search and find root nodes where the depth is equal to the max depth. Once you find one such node, then from there go up the trail of parent nodes, and add each node along the way to your linked list. When you reach the top, then the linked list has all the nodes of the largest path.

This algorithm would look like this:

  1. start at the root of the binary tree
  2. use a breadth-first or depth-first search to reach leaf nodes (nodes which do not have any children nodes)
    1. when you reach a leaf node, check it's depth:
      1. if it's not equal to the max depth, ignore it and continue with the search
      2. if it's equal to the max depth, then you've found the end node of a largest path (there could be more than one but it doesn't seem important to distinguish them at this point). go to the next step
  3. add this leaf node to the linked list, then go to it's parent
  4. repeat that last step until you reach the root node and there is no parent - then your linked list has all the nodes of a longest path.

Note that the order of the nodes is from the leaf node to the root - if you need to reverse it, you can do so after the last step.

夜空下最亮的亮点 2024-08-16 16:26:54

我们制作一个二叉树数据结构,用一些虚拟数据填充它,以深度或广度优先搜索的方式遍历树,并构造最长路径中节点的链表。

网上有大量伪代码和更多信息:

http://en.wikipedia.org/wiki/Binary_tree< /a>

http://en.wikipedia.org/wiki/Linked_list

http://en.wikipedia.org/wiki/Breadth-first_search

http://en.wikipedia.org/wiki/Depth-first_search

我假设您已经了解 Java 并且还没有开始完全从头开始?

如果您在 Java 方面遇到困难,那么 Objects First with Java 是一本很棒的书,而且< a href="http://mitpress.mit.edu/catalog/item/default.asp?ttype=2&tid=3440" rel="nofollow noreferrer">算法简介似乎是标准书关于算法,可能非常值得一看。

Well make a binary tree data structure, fill it with some dummy data, traverse the tree with a depth or breadth first search and construct a linked list of the nodes in the longest path.

There's plenty of pseudo code and further information online:

http://en.wikipedia.org/wiki/Binary_tree

http://en.wikipedia.org/wiki/Linked_list

http://en.wikipedia.org/wiki/Breadth-first_search

http://en.wikipedia.org/wiki/Depth-first_search

I assume you already understand Java and are not starting completely from scratch?

If you're struggling with Java then Objects First with Java is a great book and also Introduction to Algorithms seems to be the standard book on algorithms and may be well worth looking at.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文