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 ...
随机推荐
- 关于qt学习的一点小记录(2)
嗯...这次接了个单 要求图形界面,刚好可以巩固并学习下QT.毫不犹豫的就接了 下面记录下出现的问题: 1. QWidget和QDialog QDialog下的槽函数有accept()与reject( ...
- html天气预报小插件
<head></head> <body> <iframe width="225" scrolling="no" hei ...
- xcode6 建立 empty application
.新建一个single view application .打开 Info.plist,删除里面的 Launch screen interface file....以及 Main storyboard ...
- 高仿拉手网底部菜单实现FragmentActivity+Fragment+RadioGroup
先把欢迎页和引导页的代码上传了http://download.csdn.net/detail/u013134391/7183787不要积分的. 底部菜单条实如今4.0曾经都是用tabhost.如今基本 ...
- Cts框架解析(8)-IBuildProvider
IBuildProvider接口中定义了三个方法 /* * Copyright (C) 2010 The Android Open Source Project * * Licensed under ...
- Android加载图片OOM错误解决方式
前几天做项目的时候,甲方要求是PAD (SAMSUNG P600 10.1寸 2560*1600)的PAD上显示高分辨率的大图片. SQLITE採用BOLD方式存储图片,这个存取过程就不说了哈,网上一 ...
- vue-resource插件使用
本文的主要内容如下: 介绍vue-resource的特点 介绍vue-resource的基本使用方法 基于this.$http的增删查改示例 基于this.$resource的增删查改示例 基于int ...
- xtrabackup备份恢复测试
http://blog.chinaunix.net/uid-20682026-id-3319204.html
- 阻止IOS自动识别页面上的电话号码、email地址
之前写页面的时候碰到一个很恶心的情况,在6P上数字自动变色,后来找了一些资料: 在iOS的浏览器上,他们有时候会有一些“自作聪明”,自动把页面上的一串数字识别成电话号码,这样用户不小心点击这串数字,就 ...
- 自己对WSO2 ESB 见解
这周没想到要更新什么内容,就把我最近工作接触的WSO2 ESB简单介绍下吧. 前提: 一切文档,知识都要与官方文档为准. WSO2 ESB: http://wso2.com/products/ ...