IT TEST - 試試看,你能在40分鍾內任選語言解決以下問題嗎?

來源: delta101 2014-09-19 16:20:39 [] [舊帖] [給我悄悄話] 本文已被閱讀: 次 (2860 bytes)
本文內容已被 [ delta101 ] 在 2014-09-22 06:40:25 編輯過。如有問題,請報告版主或論壇管理刪除.
A binary tree is either an empty tree (represented by an empty pointer) or a node consisting of a single integer value and two binary trees called the left subtree and right subtree.
 
A binary tree T is given. A path in tree T is a non-empty sequence of nodes of tree T such that each consecutive node in the sequence is a subtree of its preceding node.
 
A path P in tree T is given. The amplitude of path P is the maximum difference between values of nodes on path P. The amplitude of tree T is the maximum amplitude of paths in T. If tree T is empty, then it does not have any paths and its amplitude is assumed to be 0.
 
For example, consider the following tree T consisting of nine nodes:

           5
 /  \
        8    9
       /  \   |  \
    12   2  8   4
              |    |
              2   5
 
Sequence [9, 8, 2] is a path in this tree. Sequence [12, 8, 2] is not a path in this tree. Path [5, 8, 2] has amplitude 6. Paths [9, 8, 2] and [5, 8, 12] have amplitude 7. The amplitude of the tree is 7, as no path in it has amplitude greater than [9, 8, 2] or [5, 8, 12].
 
Assume that the following declarations are given:
class Tree {
 public int x;
 public Tree l;
 public Tree r;
}
 
Write a function:
 
class Solution { public int solution(Tree T); }
 
that, given a tree T consisting of N nodes, returns the amplitude of T.
 
For example, given tree T shown in the example above, the function should return 7.
Assume that:
·    N is an integer within the range [0..1,000,000];
·    each element of tree T is an integer within the range [-1,000,000,000..1,000,000,000].
 

所有跟帖: 

簡單 -SunnyT- 給 SunnyT 發送悄悄話 (63 bytes) () 09/21/2014 postreply 01:26:56

about 35 minutes -爾思- 給 爾思 發送悄悄話 (2583 bytes) () 09/22/2014 postreply 13:27:47

Good job. I will test code later. -delta101- 給 delta101 發送悄悄話 (0 bytes) () 09/22/2014 postreply 18:58:31

請您先登陸,再發跟帖!

發現Adblock插件

如要繼續瀏覽
請支持本站 請務必在本站關閉/移除任何Adblock

關閉Adblock後 請點擊

請參考如何關閉Adblock/Adblock plus

安裝Adblock plus用戶請點擊瀏覽器圖標
選擇“Disable on www.wenxuecity.com”

安裝Adblock用戶請點擊圖標
選擇“don't run on pages on this domain”