Family Tree
Question
A traditional constructing tree problem.
Given a string to represent relationships, and print out number n level names.
For example,
Input: "Frank->Mary,Mary->Sam,Mary->Bob,Sam->Katie,Sam->Pete", 2
Output: [Mary]
Solution
Programming thinking is simple, first, we need to construct a family tree according to input, then we do BFS to find results.
Two trick points to notice.
1. Create an ancestor treenode first and all other input nodes' default parent is this ancestor node.
2. Create a map to store name and TreeNode relationship for quick check.
class TreeNode {
public String val;
public TreeNode parent;
public List<TreeNode> children;
public TreeNode(String val) {
this.val = val;
children = new ArrayList<TreeNode>();
}
}
import java.util.*;
public class Solution {
public static void main(String[] args) {
String s = "Frank->Mary,Mary->Sam,Mary->Bob,Sam->Katie,Sam->Pete";
int target = 2;
// Construct family tree
TreeNode ancestor = new TreeNode("Ancestor");
Map<String, TreeNode> map = new HashMap<String, TreeNode>();
map.put("Ancestor", ancestor);
String[] relations = s.split(",");
for (String relation : relations) {
String[] names = relation.split("->");
String parent = names[0];
String child = names[1];
TreeNode parentNode, childNode;
if (map.containsKey(parent)) {
parentNode = map.get(parent);
} else {
parentNode = new TreeNode(parent);
parentNode.parent = ancestor;
}
if (map.containsKey(child)) {
childNode = map.get(child);
} else {
childNode = new TreeNode(child);
childNode.parent = parentNode;
}
List<TreeNode> childrenList = parentNode.children;
if (!childrenList.contains(childNode))
childrenList.add(childNode);
map.put(parent, parentNode);
map.put(child, childNode);
System.out.println(parent);
System.out.println(child);
}
// Find children of ancestor
List<TreeNode> firstChildren = ancestor.children;
for (String tmp : map.keySet()) {
TreeNode tmpNode = map.get(tmp);
if (tmpNode.parent == ancestor) {
firstChildren.add(tmpNode);
System.out.println(tmpNode.val);
}
}
System.out.println("start BFS");
// BFS to get result
int level = 0;
List<TreeNode> currentList = new ArrayList<TreeNode>();
List<String> result = new ArrayList<String>();
List<TreeNode> nextList;
currentList.add(ancestor);
while (currentList.size() > 0) {
nextList = new ArrayList<TreeNode>();
for (TreeNode tmpNode : currentList) {
List<TreeNode> childrenList = tmpNode.children;
for (TreeNode oneChild : childrenList) {
if (!nextList.contains(oneChild))
nextList.add(oneChild);
}
currentList = nextList;
level++;
if (level == target) {
for (TreeNode tmpNode2 : currentList)
result.add(tmpNode2.val);
break;
}
}
}
for (String output : result) {
System.out.println(output);
}
}
}
Family Tree的更多相关文章
- [数据结构]——二叉树(Binary Tree)、二叉搜索树(Binary Search Tree)及其衍生算法
二叉树(Binary Tree)是最简单的树形数据结构,然而却十分精妙.其衍生出各种算法,以致于占据了数据结构的半壁江山.STL中大名顶顶的关联容器--集合(set).映射(map)便是使用二叉树实现 ...
- SAP CRM 树视图(TREE VIEW)
树视图可以用于表示数据的层次. 例如:SAP CRM中的组织结构数据可以表示为树视图. 在SAP CRM Web UI的术语当中,没有像表视图(table view)或者表单视图(form view) ...
- 无限分级和tree结构数据增删改【提供Demo下载】
无限分级 很多时候我们不确定等级关系的层级,这个时候就需要用到无限分级了. 说到无限分级,又要扯到递归调用了.(据说频繁递归是很耗性能的),在此我们需要先设计好表机构,用来存储无限分级的数据.当然,以 ...
- 2000条你应知的WPF小姿势 基础篇<45-50 Visual Tree&Logic Tree 附带两个小工具>
在正文开始之前需要介绍一个人:Sean Sexton. 来自明尼苏达双城的软件工程师.最为出色的是他维护了两个博客:2,000Things You Should Know About C# 和 2,0 ...
- Leetcode 笔记 110 - Balanced Binary Tree
题目链接:Balanced Binary Tree | LeetCode OJ Given a binary tree, determine if it is height-balanced. For ...
- Leetcode 笔记 100 - Same Tree
题目链接:Same Tree | LeetCode OJ Given two binary trees, write a function to check if they are equal or ...
- Leetcode 笔记 99 - Recover Binary Search Tree
题目链接:Recover Binary Search Tree | LeetCode OJ Two elements of a binary search tree (BST) are swapped ...
- Leetcode 笔记 98 - Validate Binary Search Tree
题目链接:Validate Binary Search Tree | LeetCode OJ Given a binary tree, determine if it is a valid binar ...
- Leetcode 笔记 101 - Symmetric Tree
题目链接:Symmetric Tree | LeetCode OJ Given a binary tree, check whether it is a mirror of itself (ie, s ...
- Tree树节点选中及取消和指定节点的隐藏
指定节点变色 指定节点隐藏 单击节点 未选中则选中该节点 已选中则取消该节点 前台: 1.HTML <ul id="listDept" name="listDept ...
随机推荐
- telnet 不是内部或外部 命令
win7->程序和功能->打开或关闭Windows功能->找到telnet安装下
- hdu5772-String problem(最大权闭合子图问题)
解析: 多校标答 第一类:Pij 表示第i个点和第j个点组合的点,那么Pij的权值等于w[i][j]+w[j][i](表示得分)第二类:原串中的n个点每个点拆出一个点,第i个点权值为 –a[s[i]] ...
- 利用智能手机(Android)追踪一块磁铁(一)
之前看到一个外国人用iPhone做了一个追踪磁铁的Demo感觉不错(参考视频:http://v.youku.com/v_show/id_XODM2MjczNzE2.html),然后我就参考做了一个An ...
- POJ 3046 Ant Counting DP
大致题意:给你a个数字,这些数字范围是1到t,每种数字最多100个,求问你这些a个数字进行组合(不包含重复),长度为s到b的集合一共有多少个. 思路:d[i][j]——前i种数字组成长度为j的集合有多 ...
- XNOR-Net ImageNet Classification Using Binary Convolutional Neural Networks
转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6245186.html by 少侠阿朱
- JConsole 连接配置
远程监控配置 JDK配置 在%JAVA_HOME%/jre/lib/management目录下,jmxremote.password.template.jmxremote.password需要修改配置 ...
- hdu4267 A Simple Problem with Integers
A Simple Problem with Integers Time Limit: 5000/1500 MS (Java/Others) Memory Limit: 32768/32768 K (J ...
- C++中,访问字符串的三种方法
1.用字符数组存放一个字符串 程序1:定义一个字符数组并初始化,然后输出其中的字符串. #include<iostream> using namespace std; int main() ...
- 泛泰A880S升级官方4.4.2 binx教程
之前为了尝鲜,直接官升4.4.2,红砖了.越南兄弟说官方没更新升级工具,所以导致升级到78%就停止了,他给的办法,我试也没成功.官方旧版本号又不能升级S0221118以上的版本号,新版的离线升级工具没 ...
- thinkphp框架的路径问题 - 总结
thinkphp框架的路径问题 - 总结 (2011-06-21 11:01:28) 转载▼ 标签: thinkphp 框架 路径 杂谈 分类: Php TP中有不少路径的便捷使用方法,比如模板中使用 ...