19年冬季第四题 PAT甲级 1167 Cartesian Tree (30分)超级详解不AC都难

题目:

7-4 Cartesian Tree (30分)

A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesian tree is shown by the figure.

CTree.jpg

Your job is to output the level-order traversal sequence of the min-heap Cartesian tree.
Input Specification:

Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N distinct numbers in the next line, separated by a space. All the numbers are in the range of int.
Output Specification:

For each test case, print in a line the level-order traversal sequence of the min-heap Cartesian tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:
10
8 15 3 4 1 5 12 10 18 6
Sample Output:
1 3 5 8 4 6 15 10 12 18

题目大意:

给定一个二叉最小堆(Cartesian Tree)的中序序列,输出层序遍历序列。

这道题的题眼:

1.给出的是个min-heap
最小堆,即所有父节点不大于子节点的二叉树。如果找到了目前区间的最小值,那就是个父节点。
2.给出的顺序是inorder
中序,也就是LNR。是不是想到了递归和分而治之的老方法了?
3.输出的顺序是level-order
层序。如果不建树,就是开个大数组,按照下标放;如果建树,就得用BFS遍历树了。

这道题的陷阱:

没有说是哪种二叉树,如果是最极端的那种,就是每行只有一个节点,画个草图就是:
在这里插入图片描述
4是3的左孩子,3是2的左孩子,2是1的左孩子。
题目说最多30个节点,如果不建树那得需要2的30次方那么大的数组了,那会遇到编译不通过。所以这道题还得建树。

我自己写的测试用例:

改变节点的数量和二叉树的内部结构。不过这道题本身是没啥坑的。

//输入1
8
60 58 38 52 8 82 25 70
//输出1
8 38 25 58 52 82 70 60

//输入2
13
8 4 2 9 5 10 1 11 6 12 3 7 13
//输出2
1 2 3 4 5 6 7 8 9 10 11 12 13

附上满分答案:


                
  • 5
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
`compute_cartesian_path`函数是MoveIt中一个非常有用的函数,可以用来计算机器人末端执行器的直线或圆弧路径。该函数的完整定义如下: ```python def compute_cartesian_path(self, waypoints, eef_step, jump_threshold, avoid_collisions=True, path_constraints=None, attached_object_touch_links=None, moveit_msgs_opts=None): """ Compute a sequence of waypoints that make the end-effector move in straight line segments that follow the poses specified as a list of waypoints. :param waypoints: A list of waypoints that specify the poses for the end-effector to move through. Each waypoint is specified as a `geometry_msgs.msg.PoseStamped` message. :param eef_step: The distance in meters between consecutive waypoints on the returned path. This value effectively controls the granularity of the computed path. :param jump_threshold: The maximum distance in configuration space that the end-effector is allowed to travel in a single step. If the distance between consecutive configurations along the resulting path exceeds this value, the path is considered invalid and will not be returned. :param avoid_collisions: Whether to check for collisions between the robot and the environment as the path is being computed. If collisions are detected, the computed path will be modified to avoid the obstacles. :param path_constraints: A `moveit_msgs.msg.Constraints` message that specifies constraints to be enforced on the computed path. If `None`, no constraints will be used. :param attached_object_touch_links: A list of link names that specify which links the attached object is allowed to touch during the motion. If `None`, the default value of the `touch_links` parameter in the `planning_scene_interface` constructor will be used. :param moveit_msgs_opts: Advanced options for the computation of the path. This is a dictionary of key-value pairs, where the keys are strings and the values are either integer, float or string values. The available options are the same as those that can be set in the `moveit_msgs.msg.RobotTrajectory` message. :returns: `(plan, fraction)` where `plan` is a `moveit_msgs.msg.RobotTrajectory` message that describes the computed path, and `fraction` is a float value between 0.0 and 1.0 that specifies the fraction of the path that was successfully computed. If `fraction` is less than 1.0, the returned plan should be considered invalid. """ ``` 其中,参数的含义如下: - `waypoints`:一个包含机器人末端执行器路径的列表,每个路径点都是一个`geometry_msgs.msg.PoseStamped`消息。 - `eef_step`:机器人末端执行器在计算出的路径中使用的步长,以米为单位,控制路径的粒度。 - `jump_threshold`:在计算出的路径中允许的连续步长之间的最大距离,以配置空间中的距离为单位。如果连续的配置之间的距离超过此值,则该路径被认为是无效的,不会返回。 - `avoid_collisions`:在计算路径时是否检查机器人与环境之间的碰撞。如果检测到碰撞,则计算出的路径将被修改以避免障碍物。 - `path_constraints`:一个`moveit_msgs.msg.Constraints`消息,指定要强制执行的计算路径上的约束条件。如果为`None`,则不使用任何约束。 - `attached_object_touch_links`:一个字符串列表,指定附加对象在运动过程中允许触摸的链接。如果为`None`,则将使用`planning_scene_interface`构造函数中`touch_links`参数的默认值。 - `moveit_msgs_opts`:计算路径的高级选项。这是一个键值对字典,其中键是字符串,值可以是整数、浮点数或字符串。可用选项与可以在`moveit_msgs.msg.RobotTrajectory`消息中设置的选项相同。 该函数的返回值为一个元组`(plan, fraction)`,其中`plan`是一个`moveit_msgs.msg.RobotTrajectory`消息,描述了计算出的路径,`fraction`是介于0.0和1.0之间的浮点值,指定成功计算的路径的比例。如果`fraction`小于1.0,则返回的计划应视为无效。 该函数通过计算机器人末端执行器的直线或圆弧路径,生成一条路径,将机器人从起始点运动到目标点。在计算路径时,可以指定一些约束条件,例如机器人与环境之间的碰撞,机器人的关节角度等。这使得计算出的路径更加精确和安全。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值