POJ2499 -Binary Tree

题意

一个二叉树某个结点为(a,b),其左孩子为(a + b, b),右孩子为 (a, a + b),根节点为(1,1)。给某一结点,问从根节点到此结点需要向左多少步向右多少步

思路

给出某个节点(a,b),若a>b则为左孩子,相反则为右孩子,并由此可以推出其父节点,按照这个规律一直推到(1,1)。若(a,b)是由根节点向左L向右R,则(b,a)为根节点向左R向右L。

推过样例之后就能发现规律啦

总结

水题,output有空行忘记了,WA了一次,但是现在A完之后不显示A了是什么鬼啊,生气

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <queue>
 4 #include <string>
 5 #include <iostream>
 6
 7 using namespace std ;
 8 int T, kase = 0, a, b;
 9
10 int main()
11 {
12   //  freopen("in.txt", "r", stdin);
13     scanf("%d", &T);
14     while(T--)
15     {
16         scanf("%d %d", &a, &b);
17         int Left = 0, Right = 0;
18         bool flag = false;
19         bool Reverse = false;
20         if(a < b) {
21             Reverse = true;
22             swap(a, b);
23         }
24 //左右左,左是false
25         while(b)
26         {
27             int x = a, y = b;
28             if(!flag)
29             {
30                 Left += a / b;
31             }
32             else
33             {
34                 Right += a / b;
35             }
36             flag = !flag;
37             a = y;
38             b = x % y;
39         }
40         if(flag) Left--;
41         else Right--;
42         printf("Scenario #%d:\n", ++kase);
43         if(Reverse)
44             printf("%d %d\n", Right, Left);
45         else
46             printf("%d %d\n", Left, Right);
47         printf("\n");
48     }
49     return 0 ;
50 }
时间: 2024-11-01 18:13:07

POJ2499 -Binary Tree的相关文章

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 # Definition for a binary tre

226反转二叉树 Invert Binary Tree

Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can't invert a binary tree on a wh

[leetcode] 104. 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. 递归遍历 左子树 和 右子树 一刷: public int maxDepth(TreeNode root) { if(root == null){ return 0; } int

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 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: Recur

LeetCode Binary Tree Inorder Traversal

LeetCode解题之Binary Tree Inorder Traversal 原题 不用递归来实现树的中序遍历. 注意点: 无 例子: 输入: {1,#,2,3} 1 2 / 3 输出: [1,3,2] 解题思路 通过栈来实现,从根节点开始,不断寻找左节点,并把这些节点依次压入栈内,只有在该节点没有左节点或者它的左子树都已经遍历完成后,它才会从栈内弹出,这时候访问该节点,并它的右节点当做新的根节点一样不断遍历. AC源码 # Definition for a binary tree node

Java [Leetcode 107]Binary Tree Level Order Traversal II

题目描述: Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For example:Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its bottom-up level order

Lowest Common Ancestor of a Binary Tree

题目连接 https://leetcode.com/problems/lowest-common-ancestor-of-a-binary-tree/ Common Ancestor of a Binary Tree Description Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. According to the definition of LCA on

Invert Binary Tree

package cn.edu.xidian.sselab; /** * title:Invert Binary Tree * content: * nvert a binary tree.  *     4 *   /   \ *  2     7 * / \   / \ *1   3 6   9 *to *     4 *   /   \ *  7     2 * / \   / \ *9   6 3   1 */public class InvertBinaryTree { /**    

leetcode笔记:Minimum Depth of Binary Tree

一. 题目描述 Given a binary tree, find its minimum depth. The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 二. 题目分析 这道题属于二叉树的深度优先搜索,然后返回深度最小的值,可以递归(当然,也可以使用迭代)来实现.递归退出的条件是到达叶子节点或者到达空子树,使用空子树