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,1),对每个结点(a,b)其左结点为 (a + b, b) ,其右结点为 (a, a + b),已知某结点坐标,求根节点到该节点向左和向右走的次数. 分析:往回一步一步走肯定超时,不过如果a>b,大的有点多,就没必要一步一步走,直接a/b就好了,然后把a变成a%b取余,那么a/b就是从现在的a到原来的a向左走的步数.(a<b同理) 同理,如果a=1的话,那么也没必要往回走了,因为从根节点到现在的结点就是向右走了b-1.(b=1同理) #pragma comment(…
题意:给一个这样的二叉树,每个节点用一对数(a,b)表示,根节点为(1,1).设父亲为(a,b),左儿子(a+b,b),右儿子(a,a+b). 给几组数据,(i,j),求从根节点到(i,j)节点需要向左子树走多少次,往右子树多少次.思路:可以发现:当i>j时,(i,j)为左儿子;当i<j时,(i,j)为右儿子.    设父节点为(a,b),往左子树需要走l次,往右子树需要走r次,那么:    1.i>j时:i=a+b,j=b -> a=i-j,b=j l++;    2.i<…
Binary Tree Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6355   Accepted: 2922 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 node…
前言 第一天的算法都还没有缓过来,直接就进入了第二天的算法学习.前一天一直在整理Binary Search的笔记,也没有提前预习一下,好在Binary Tree算是自己最熟的地方了吧(LeetCode上面Binary Tree的题刷了4遍,目前95%以上能够Bug Free)所以还能跟得上,今天听了一下,觉得学习到最多的,就是把Traverse和Divide Conquer分开来讨论,觉得开启了一片新的天地!今天写这个博客我就尽量把两种方式都写一写吧. Outline: 二叉树的遍历 前序遍历t…
B - Binary Tree   Description The Old Frog King lives on the root of an infinite tree. According to the law, each node should connect to exactly two nodes on the next level, forming a full binary tree. Since the king is professional in math, he sets…
[抄题]: We are given a binary tree (with root node root), a target node, and an integer value K. Return a list of the values of all nodes that have a distance K from the target node.  The answer can be returned in any order. Example 1: Input: root = [3…
[抄题]: You need to construct a binary tree from a string consisting of parenthesis and integers. The whole input represents a binary tree. It contains an integer followed by zero, one or two pairs of parenthesis. The integer represents the root's valu…
[抄题]: Given a binary tree where every node has a unique value, and a target key k, find the value of the nearest leaf node to target k in the tree. Here, nearest to a leaf means the least number of edges travelled on the binary tree to reach any leaf…
[抄题]: Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that duplicates do not exist in the tree. For example, given inorder = [9,3,15,20,7] postorder = [9,15,7,20,3] Return the following binary tree: 3 /…