Is this a tree?
群友分的一OA道题,给定数组,数组中的每个元素都是一个pair对,(父节点值,子节点值),
让重建一个二叉树;
思路:
1.首先找到根节点:
在父节点中出现过且在子节点中没有出现过的值即为根节点的值
2.建树
要用dict来保存已经创建好的节点,对于新的pair对,父节点和子节点都要单独判断是否已经创建;
因为父节点没有创建的时候可能子节点已经创建了,这时候就要复用已经创建的子节点;
子节点没有创建的时候可能父节点已经创建了,这时候要复用已经创建的父节点;
注意一个父节点最多只能有两个子节点;
class TreeNode:def __init__(self, val):self.val = valself.left = Noneself.right = None
array = [['A', 'B'], ['A', 'C'], ['B', 'G'], ['C', 'H'], ['E', 'F'], ['B', 'D'], ['C', 'E']]
root_val = set([item[0] for item in array]) - set([item[1] for item in array])
root_val = list(root_val)[0]def buildTree(array, root_value):dictionary = {}for parent, child in array:if parent not in dictionary:p_node = TreeNode(parent)if child in dictionary:c_node = dictionary[child]else:c_node = TreeNode(child)dictionary[child] = c_nodedictionary[parent] = p_nodep_node.left = c_nodeelse:if child in dictionary:c_node = dictionary[child]else:c_node = TreeNode(child)dictionary[child] = c_nodeif dictionary[parent].left and dictionary[parent].right:return "error"else:if dictionary[parent].left is None:dictionary[parent].left = c_nodeelse:dictionary[parent].right = c_noderoot = dictionary[root_value]return rootdef preorder_traverse(root):if root is None:return ""left, right = preorder_traverse(root.left), preorder_traverse(root.right)return "(%s%s%s)"%(root.val, left, right)root = buildTree(array, root_val)
if root != "error":print(preorder_traverse(root))
else:print("error")
Is this a tree?相关推荐
- 107. Binary Tree Level Order Traversal II
题目 Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from l ...
- 102. Binary Tree Level Order Traversal
题目 Binary Tree Level Order Traversal 层次遍历二叉树 链接 Given a binary tree, return the level order traversa ...
- Python---哈夫曼树---Huffman Tree
今天要讲的是天才哈夫曼的哈夫曼编码,这是树形数据结构的一个典型应用. !!!敲黑板!!!哈夫曼树的构建以及编码方式将是我们的学习重点. 老方式,代码+解释,手把手教你Python完成哈夫曼编码的全过程 ...
- [Java]LeetCode297. 二叉树的序列化与反序列化 | Serialize and Deserialize Binary Tree
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ ➤微信公众号:山青咏芝(shanqingyongzhi) ➤博客园地址:山青咏芝(https://www.cnblog ...
- Code Forces Bear and Forgotten Tree 3 639B
B. Bear and Forgotten Tree 3 time limit per test2 seconds memory limit per test256 megabytes inputst ...
- Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈)
Codeforces Round #417:E. FountainsSagheer and Apple Tree(树上博弈) 标签: codeforces 2017-06-02 11:41 29人阅读 ...
- datagrid底部显示水平滚动_DevExpress WPF v19.1:Data Grid/Tree List等控件功能增强
行业领先的.NET界面控件DevExpress 日前正式发布v19.1版本,本站将以连载的形式介绍各版本新增内容.在本系列文章中将为大家介绍DevExpress WPF v19.1中新增的一些控件及部 ...
- java easyui tree例子_EasyUI Tree的简单使用
此前写过zTree插件的demo,没有记录下来,这次记录一下EasyUI的Tree. 实现效果:获取数据库表的数据,以树结构的形式展示出来. 树结构数据分为同步加载和异步加载,同步加载就是初始化加载时 ...
- windows用 tree命令查看目录文件夹结构
windows用 tree命令查看目录文件夹结构 ## 查看帮助 tree --helptree --dirsfirst --filelimit 6 -h -t –dirsfirst 目录优先展示 – ...
- PCL :K-d tree 2 结构理解
K-d tree 基础思路:(先看之前的KNN思想,更容易理解) 导语:kd 树是一种二叉树数据结构,可以用来进行高效的 kNN 计算.kd 树算法偏于复杂,本篇将先介绍以二叉树的形式来记录和索引空间 ...
最新文章
- ACMNO.31 C语言-宏求余 输入两个整数,求他们相除的余数。用带参的宏来实现,编程序。
- 《战狼2》中人脸识别无人机表现不俗,军、警用果真如此高能?
- oracle表分析 示例
- Linux与Windows共享文件夹之samba的安装与使用 资源管理器直接查看服务器文件
- 华为v3鸿蒙系统_重磅!华为鸿蒙系统问世!
- C语言的本质(19)——预处理之一:宏定义
- matlab小波分解图像,MATLAB小波图像分解
- Google关停中国区域翻译服务后继续使用Chrome自带翻译插件的方法教程
- OpenSIPS脚本中的flag
- 边界路由linux,路由表构成简介(Destination/Gateway/Genmask/Iface)
- 腾讯android一键root工具,腾讯一键Root工具
- pg_repack安装及使用
- linux-一条命令,找出包含指定字符串的进程并杀死
- 求链表中的中点、上中点、下中点
- [英语竞赛] 知识整理
- 第四章 虚拟现实的计算体系结构
- Runtime.getRuntime().availableProcessors()
- 电信高清语音服务器,电信VoLTE高清通话来了,来电秒接通、打电话4G不掉线!...
- Win10蓝牙设备删除后无法连接解决办法
- 介于传统巨石服务与微服务之间的一种选择