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 ...
随机推荐
- C# 通过线程更新UI
摘自:http://msdn.microsoft.com/zh-cn/library/ms171728(en-us,VS.80).aspx 关键代码(form中增加): delegate void S ...
- 捆绑和缩小(BundleConfig.RegisterBundles) 第五章 : MVC中的使用 | {version}用法
使用捆绑与 ASP.NET MVC 放缩法 在这一节我们将创建 ASP.NET MVC 项目,审查捆绑和缩小.首先,创建一个新的 ASP.NET MVC 互联网项目,命名为MvcBM ,而无需更改任何 ...
- postgresql C/C++ API 接口
1,postgresql学习uri推荐 http://www.php100.com/manual/PostgreSQL8/ http://www.php100.com/manual/PostgreSQ ...
- hdu 4336 Card Collector(期望 dp 状态压缩)
Problem Description In your childhood, people in the famous novel Water Margin, you will win an amaz ...
- [HeadFirst-HTMLCSS入门][第九章盒模式]
新属性 line-height 行间距 line-height: 1.6em; border 边框 属性值 solid 实线 double 双实线 groove 一个槽 outset 外凸 inset ...
- CPU性能测试
用计算圆周率的办法来测试cpu性能 4*a(1) 是 bc 主動提供的一個計算 pi 的函數,至於 scale 就是要 bc 計算幾個小數點下位數的意思.當 scale 的數值越大, 代表 pi 要被 ...
- sizeof对int long double char的使用
主要针对int long char double 字节长度的识记. 1 #include <stdio.h> 2 3 int main() 4 { 5 int a[100]; 6 int ...
- 配置Tomcat中的Context元素中的中文问题
发布一个名叫helloapp的web应用,helloapp位于D:\我\helloapp.发布的方式是通过配置<CATALINA_HOME>/conf/Catalina/localhost ...
- MySQL 初学笔记 ① -- MySQL用户登录权限控制
1. MySQL 登录 MySQL -u username -p 2. MySQL 创建用户 use mysql //进入mysql 表 INSERT INTO user (Host,User,Pas ...
- SSE2 Intrinsics各函数介绍[转]
SIMD相关头文件包括: //#include <ivec.h>//MMX //#include <fvec.h>//SSE(also include ivec.h) //#i ...