- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我有这两个二叉树序列(不是 BSD):
顺序:3 2 1 4 5 7 6
后序:3 1 2 5 6 7 4
我们知道postOrder中的最后一个元素是根,所以我们把根定位在inOrder序列上,这样 segmentation :
- 根左侧的所有元素都转到左子树
- 根右侧的所有元素都进入右子树此外,它们在树中出现的顺序由 postOrder 给出。
对于这个例子:4 = 根
左子树(有序):3 2 1 右子树(有序):5 7 6
左子树(后序):3 1 2 右子树(后序):5 6 7
我们递归地做同样的事情......所以,我认为重建的树是:
4
2 7
3 1 5 6
我只想返回导致最短路径(总和)的叶子;我不需要重建树然后遍历它并做最短路径。我只需要引导我达到最小总和的叶子。在这种情况下,我有 4 条可能的路径:4+2+3=9 || 4+2+1 = 7 || 4+7+5 = 16 || 4+7+6 = 17,少的是7,必须还叶1。
我觉得这个算法很简单,但是我很难为它写递归代码......
有人可以帮助菜鸟吗? C、Java、C++ 或 Python ...我不介意
最佳答案
如果您对一般二叉树进行中序遍历和后序遍历,如果您可以有重复的值,它们甚至不会唯一地定义树,例如后序和中序的序列 [1,1,1] 可以是以下树之一:
1 1
1 1 1
1
最小和路径左边是2的和,右边是3的和。
因此假设所有值都是不同的。
假设你有一个后序遍历列表 [x1,x2,...,xn] 和中序遍历列表 [y1,...,yk,...,yn] 使得 xn==yk .因为 xn 是树的根,所以您现在知道 [y1,...,yk-1] 是左子树,而 [yk+1,...,yn] 是右子树。然后左子树也用[x1,...,xk-1]表示,因为左子树的大小显然是常数,所以你也可以将后序遍历列表划分为xk-1和xk。
例如,这是一个没有任何特定顺序的非平衡二叉树:
5
3 6
9 2 1
4
中序遍历为[9,3,2,5,6,4,1],后序为[9,2,3,4,1,6,5]。
树将像这样递归构造:取后序遍历的最后一个元素 (5);按顺序划分为 [9,3,2] 和 [6,4,1](在元素 5 的位置划分);因此,仅基于现在已知的子树大小,后序遍历列表被拆分为 [9,2,3] 和 [4,1,6]。然后递归继续;让我们看看 [6,4,1] 树,因为它不平衡:
根是6;在中序 [6,4,1] 中,左子树为空,右子树为 [4,1],因此从后序列表 [4,1,6] 中,您将 [] 作为左子树subtree 和 [4,1] 作为右子树;从那里你得到根节点 1,你发现 [4] 是左子树;从中得到形状
6
1
4
根据需要。
现在因为您的树没有排序、平衡等,您可以尝试编写递归代码来处理您的查询。这是在 C++ 中:
const int size = 7; /* Size of the tree */
/* Given arrays */
int post_order[size] = { 3 , 1 , 2 , 5 , 6 , 7 , 4 };
int in_order[size] = { 3 , 2 , 1 , 4 , 5 , 7 , 6 };
/* Variables updated during recursion */
int min_sum = 99999999; /* not initialized */
int best_leaf = -1; /* not initialized */
/* Recursive descent */
/* prb = post-order range begin, irb = in-order range begin, etc. */
void min_sum_leaf(int acc, int prb, int irb, int len) {
if (len == 0) return; /* empty subtree */
if (len == 1) { /* leaf */
int sum = acc + in_order[irb];
if (sum<min_sum) { min_sum = sum; best_leaf = in_order[irb]; }
return;
}
/* non-leaf */
int subtree_root = post_order[prb + len - 1];
/* find the size of the left subtree */
int i;
for (i=0;i<len;i++) {
if (in_order[irb + i] == subtree_root) break;
}
/* Now i is the length of the left subtree, len - i - 1 of the right */
min_sum_leaf(acc + subtree_root, prb, irb, i);
min_sum_leaf(acc + subtree_root, prb + i, irb + i + 1, len - i - 1);
}
/* Driver */
int find_min_sum_leaf() {
min_sum = 99999999; best_leaf = -1;
min_sum_leaf(0, 0, 0, size);
return best_leaf;
}
注意:我没有编译或运行算法,但逻辑应该在那里!
关于Java/C/C++ :find leaf of the shortest path of a binary tree without reconstructing tree (help with recursion),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2253809/
问题:我有一个二叉树,所有叶子都编号(从左到右,从 0 开始)并且它们之间不存在连接。 我想要一种算法,给定两个索引(2 个不同的叶子),从较大的叶子(索引较高的叶子)开始访问树并到达较低的叶子。 树
我需要在 AST 中表示这样的结构: struct { int data; double doubleDataArray[10]; struct { int nestedData;
我有一个自相关表 myTable,如下所示: ID | RefID ---------- 1 | NULL 2 | 1 3 | 2 4 | NULL 5 | 2 6 | 5 7 | 5
我在使用此数据结构模型的类别中有一个表。还使用另一个表向我显示类别分支的路径: **t_category** id | name | parent ------------------ 1 mas
ExtractEveryPack::type是 Pack 中所有“叶包”的包.例如,ExtractEveryPack, int, Pack> >::type是Pack, Pack > .但“外包装”不
我写了下面的代码来构造一个给定顶点的树,给出顶点之间的连接列表。 type Connection = (Int,Int) data Tree = Leaf Int | Node Int [Tree]
我正在尝试识别树中的“叶子”,但我很困惑为什么我的查询没有给我想要的东西。 问题是这样的: 所以我的想法是,只要id不在p_id列中,那么它就应该是“Leaf” select id, 'Leaf' a
我正在尝试识别树中的“叶子”,但我很困惑为什么我的查询没有给我想要的东西。 问题是这样的: 所以我的想法是,只要id不在p_id列中,那么它就应该是“Leaf” select id, 'Leaf' a
我需要做什么来渲染 TreeMap ,使节点向图的叶侧对齐? 我想采用通常看起来像的任意 TreeMap : 并让它类似于: 最佳答案 一种方法是发现树的深度并设置 depth每个没有该值的子节点的节
我从一个几乎空白的项目开始,我可以使用以下方法从 welcome.leaf 渲染一个简单的页面: router.get("view") { req -> Future in let leaf
我正在尝试将骨架模板 View 添加到最近的 Vapor 2 应用程序中,到目前为止,该应用程序仅使用 MySQL 数据库生成 JSON 输出。如果我使用以下最少的代码: get("viewT
题目地址:https://leetcode.com/problems/sum-root-to-leaf-numbers/description/ 题目描述: Given a binary tree
这个查询: SELECT payload.pages FROM FLATTEN([publicdata:samples.github_nested] , payload) ORDER BY creat
我已经在 Fossil SCM 工作了一段时间,但我仍然看到一些我不太明白的东西。 在屏幕截图中,您可以看到我在存储库中有两个叶子,但遗憾的是我找不到将它们合并回主干的方法(在我的所有提交中都有“叶子
给定一棵仅包含 0-9 数字的二叉树,每个根到叶的路径都可以代表一个数字。 一个例子是根到叶的路径 1->2->3 代表数字 123。 找出所有根到叶数的总和 % 1003。 例子: 如果 1 是根节
我使用 vapor 加载 html 或 leaf,它给我错误消息“500”。服务器日志显示给我 [Data File Error: unable to load file at path /Users
我有一个Group、User 和一个App 模型。在我的组模型中,我有一个属性 var apps: [App] 并且我创建了组和用户的兄弟关系。 在我的 WebsiteController 中,我有
我为此编写的代码: sumBST(BST *root) { static sum =0; if (root!= null) {
我想从 jsTree 获取所有叶节点(节点的 ID 和文本)? 我没有使用复选框 ui:jsTree。 Root -----A -----A1
我需要在 Swift Vapor 应用程序中创建一个复杂的 html 表格。 问题是:Leaf 似乎不支持像 #(somevar += 1) 这样的变量计数,也不支持像 #(somevar1 + so
我是一名优秀的程序员,十分优秀!