x******u 发帖数: 259 | 1 运气不好,碰到老印三哥三姐,悲剧了。电话进来Late了。然后讨论简历。。。
上题,头有点晕。。。把题弄错了。一开始思路不对,就悲剧了。
Given a list of child->parent relationships, build a binary tree out of it.
All the element Ids inside the tree are unique.
Example:
Given the following relationships:
Child Parent IsLeft
15 20 true
19 80 true
17 20 false
16 80 false
80 50 false
50 null false
20 50 true
You should return the following tree:
50
/
20 80
/ /
15 17 19 16
Function Signature
/**
* Represents a pair relation between one parent node and one child node
inside a binary tree
* If the _parent is null, it represents the ROOT node
*/
public class Relation {
public Integer _parent;
public Integer _child;
public boolean _isLeft;
}
/**
* Represents a single Node inside a binary tree
*/
public class Node {
public Integer _id;
public Node _left;
public Node _right;
}
/**
* Implement a method to build a tree from a list of parent-child
relationships
* And return the root Node of the tree
*/
public Node buildTree (List data)
{
//TODO
} | f*******w 发帖数: 1243 | 2 id是unique的,那就挨个读把node pointer存到hash里,id是key呗 | l*****a 发帖数: 14598 | 3 thanks for sharing.
搞个Map存结果
if Parent==null then it is ROOT.
.
【在 x******u 的大作中提到】 : 运气不好,碰到老印三哥三姐,悲剧了。电话进来Late了。然后讨论简历。。。 : 上题,头有点晕。。。把题弄错了。一开始思路不对,就悲剧了。 : Given a list of child->parent relationships, build a binary tree out of it. : All the element Ids inside the tree are unique. : Example: : Given the following relationships: : Child Parent IsLeft : 15 20 true : 19 80 true : 17 20 false
| x******u 发帖数: 259 | |
|