POJ 2499 Binary Tree 题解

本题使用所谓的辗转相除法。

还需要逆过来遍历二叉树。可以想象给出的数据点是根节点,然后遍历到根节点(1,1)。

考的是根据给出的规则,总结规律的能力。

#include <stdio.h>

namespace BinaryTree2499_1
{

int main()
{
	int T, a, b, le, ri;
	scanf("%d", &T);
	for (int t = 1; t <= T; t++)
	{
		scanf("%d %d", &a, &b);
		le = 0, ri = 0;
		while (a != 1 || b != 1)
		{
			if (a == 1 || b == 1)
			{
				if (a == 1)
				{
					ri += b - a;
					b = 1;
				}
				else
				{
					le += a - b;
					a = 1;
				}
			}
			else if (a < b)
			{
				ri += b / a;
				b %= a;
			}
			else
			{
				le += a / b;
				a %= b;
			}
		}
		printf("Scenario #%d:\n%d %d\n\n", t, le, ri);
	}
	return 0;
}

POJ 2499 Binary Tree 题解,布布扣,bubuko.com

时间: 2024-10-05 21:49:21

POJ 2499 Binary Tree 题解的相关文章

POJ 2499 Binary Tree 数学题解

Description Background Binary trees are a common data structure in computer science. In this problem we will look at an infinite binary tree where the nodes contain a pair of integers. The tree is constructed like this: The root contains the pair (1,

Poj 2499 Binary Tree

题目链接:http://poj.org/problem?id=2499 思路: 结点向左边移动时结点(a, b)变为( a+b, b),向右边移动时( a, b )变为( a, a + b); 为求最短路径<a1, a2, a3,...,an>,考虑从已经知道的结点(a, b)开始找出最短路径回到根节点(1, 1): 即向左移动次数和向右移动次数最少回到根节点,由贪心算法,若 a>b 时,a 减少最大即减去 b: 若 a < b,b 减少最大即减去a值,循环直到到达根节点(1, 1

LeetCode: 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. 题解: 题意比较清楚, 找到从root出发最长的一条路径的长度. 采用DFS即可. 相似的一道题: Minimux Depth of Binary Tree 解法: http://

leetcode Balanced Binary Tree 题解

题目描述 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 思路 判断一棵树是不是平衡二叉树,递归的求每个结点的左子树和

Lowest Common Ancestor of a Binary Tree题解

这是LeetCode上的一道题,让我们先来看一看题目: 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 nod

[Leetcode Week14]Maximum Binary Tree

Maximum Binary Tree 题解 原创文章,拒绝转载 题目来源:https://leetcode.com/problems/maximum-binary-tree/description/ Description Given an integer array with no duplicates. A maximum tree building on this array is defined as follow: The root is the maximum number in

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

leetcode题解:Binary Tree Postorder Traversal (二叉树的后序遍历)

题目: Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

LeetCode102 Binary Tree Level Order Traversal Java题解

题解: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level order traversal as: [ [3], [9,20], [15,7] ] 解