UVa548 Tree (二叉树)

链接:http://acm.hust.edu.cn/vjudge/problem/19105分析:由中序遍历和后序遍历可以唯一确定一棵二叉树。后序遍历中最后一个元素就是树根,然后在中序遍历中找到它,从而可以找出以它为根结点的左右子树的结点列表,然后再递归构造左右子树。这道题由于权值各不相同,且都是小于10000的正整数,可以以权值作为结点编号建树。边建树边统计最优解,best为最优解叶子结点权值也即叶子结点编号,best_sum为最优权值和,这两个变量作为全局变量,因为每个可行解都要与当前最优情况比较然后更新最优情况,将sum作为参数传入简化了回溯。
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <sstream>
 4 using namespace std;
 5
 6 const int maxv = 10000 + 5;
 7
 8 int n;
 9 int in_order[maxv], post_order[maxv], lch[maxv], rch[maxv];
10
11 bool read_list(int* a) {
12     string line;
13     if (!getline(cin, line)) return false;
14     stringstream ss(line);
15     n = 0;
16     int x;
17     while (ss >> x) a[n++] = x;
18     return n > 0;
19 }
20
21 int best_sum, best;
22 int build(int L1, int R1, int L2, int R2, int sum) {
23     if (L1 > R1) return 0;
24     int root = post_order[R2];
25     sum += root;
26     int p = L1;
27     while (in_order[p] != root) p++;
28     int cnt = p - L1;
29     lch[root] = build(L1, p - 1, L2, L2 + cnt - 1, sum);
30     rch[root] = build(p + 1, R1, L2 + cnt, R2 - 1, sum);
31     if (!lch[root] && !rch[root])
32         if (sum < best_sum || (sum == best_sum && root < best)) {
33             best = root; best_sum = sum;
34         }
35     return root;
36 }
37
38 int main() {
39     while (read_list(in_order)) {
40         read_list(post_order);
41         best_sum = 1000000000;
42         build(0, n - 1, 0, n - 1, 0);
43         cout << best << endl;
44     }
45 }
				
时间: 2024-10-10 00:49:49

UVa548 Tree (二叉树)的相关文章

UVA548——Tree

Tree You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path.Input The i

4.7 Lowest Common Ancestor of a Binary Search Tree 二叉树的最小共同父节点

4.7 Design an algorithm and write code to find the first common ancestor of two nodes in a binary tree. Avoid storing additional nodes in a data structure. NOTE: This is not necessarily a binary search tree. LeetCode上的原题,请参见我之前的博客Lowest Common Ancest

Leetcode 101 Symmetric Tree 二叉树

判断一棵树是否自对称 可以回忆我们做过的Leetcode 100 Same Tree 二叉树和Leetcode 226 Invert Binary Tree 二叉树 先可以将左子树进行Invert Binary Tree,然后用Same Tree比较左右子树 而我的做法是改下Same Tree的函数,改动的是第27行 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNo

UVALive 6577 Binary Tree 二叉树的LRU串

今天继续攒人品...真开心啊O(∩_∩)O~~各种身体不舒服~~ https://icpcarchive.ecs.baylor.edu/external/65/6577.pdf 题意是这样的,现在有一个向下无限延伸的二叉树.然后输入起点(通过只含LRU的字符串S,从根结点开始执行).LRU分别表示往左儿子走,往右儿子走,往爹娘处走(根结点的爹娘是自己,估计他是石头里蹦出来的). 然后输入一个可选步骤串T.可以选择T中的子序,从起点开始走.然后问可以走到多少个不同的结点. 比赛的时候不会做啊╮(╯

LeetCode | 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径【Python】

LeetCode 1372. Longest ZigZag Path in a Binary Tree二叉树中的最长交错路径[Medium][Python][DFS] Problem LeetCode Given a binary tree root, a ZigZag path for a binary tree is defined as follow: Choose any node in the binary tree and a direction (right or left). I

UVA548 Tree (二叉树的遍历)

You are to determine the value of the leaf node in a given binary tree that is the terminal node of a path of least value from the root of the binary tree to any leaf. The value of a path is the sum of values of nodes along that path. Input The input

Maximum Depth of Binary Tree 二叉树的深度

Given a binary tree,find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 此题是经典的求二叉树深度深度题目,可采用递归的方式,以此求每个节点的左子树深度和右子树深度,然后返回该节点左右子树深度最大的那个即为该节点的深度 具体代码如下: 1 /** 2 *

Maximum Depth of Binary Tree 二叉树的最大深度

Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 求二叉树的最大深度问题用到深度优先搜索DFS,递归的完美应用,跟求二叉树的最小深度问题原理相同.代码如下: /** * Definition for binary tree * s

[LeetCode] Lowest Common Ancestor of a Binary Tree 二叉树的最小共同父节点

Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on Wikipedia: “The lowest common ancestor is defined between two nodes v and w as the lowest node in T that has both v and w