"""
一、题目
实现一个字典树or前缀树,包含insert、search和startswith功能
"""classNode():def__init__(self):
self.childs =[None]*26
self.isLeaf =FalseclassTrie(object):def__init__(self):"""
Initialize your data structure here.
"""
self.root = Node()definsert(self, word):"""
Inserts a word into the trie.
:type word: str
:rtype: void
"""
self.inserthelper(word, self.root)definserthelper(self, word, node):if node == Node:returniflen(word)==0:
node.isLeaf =Truereturn
index =ord(word[0])-ord('a')if node.childs[index]isNone:
node.childs[index]= Node()
self.inserthelper(word[1:], node.childs[index])defsearch(self, word):"""
Returns if the word is in the trie.
:type word: str
:rtype: bool
"""return self.searchhepler(word, self.root)defsearchhepler(self, word, node):if node isNone:returnFalseiflen(word)==0:return node.isLeaf
index =ord(word[0])-ord('a')return self.searchhepler(word[1:], node.childs[index])defstartsWith(self, prefix):"""
Returns if there is any word in the trie that starts with the given prefix.
:type prefix: str
:rtype: bool
"""return self.startsWithhelper(prefix, self.root)defstartsWithhelper(self, prefix, node):if node isNone:returnFalseiflen(prefix)==0:returnTrue
index =ord(prefix[0])-ord('a')return self.startsWithhelper(prefix[1:], node.childs[index])#if __name__ == '__main__':# trie = Trie()# trie.insert("apple")# print(trie.search("apple"))# print(trie.search("app"))# print(trie.startsWith("app"))# trie.insert("app")# print(trie.search("app"))
树的遍历
"""
一、题目
按层次的遍历的方法打印一颗树
例如有如下一棵树:
5
/ \
3 6
/ \ \
2 4 7
打印结果是
5 3 6 2 4 7
二、思路
层次遍历的步骤是:
对于不为空的结点,先把该结点加入到队列中
从队中拿出结点,如果该结点的左右结点不为空,就分别把左右结点加入到队列中
重复以上操作直到队列为空
"""classTreeNode:def__init__(self, x):
self.val = x
self.left =None
self.right =Nonedefhierarchical_traversal(root):if root isNone:return[]# 用一个list和一个索引index模拟队列的先进先出
queue =[]
index =0
result =[]
queue.append(root)whilelen(queue)> index:
temp = queue[index]
index +=1
result.append(temp.val)if temp.left isnotNone:
queue.append(temp.left)if temp.right isnotNone:
queue.append(temp.right)return result
defin_order_traversal(root):# 中序
res =[]
stack =[]if root isNone:return res
cur = root
whilelen(stack)!=0or cur isnotNone:while cur isnotNone:
stack.append(cur)
cur = cur.left
node = stack.pop()
res.append(node.val)
cur = node.right
return res
defpre_order_traversal(root):# 前序
result =[]
stack =[]
stack.append(root)whilelen(stack)!=0:
node = stack.pop()if node isNone:continue
result.append(node.val)
stack.append(node.right)
stack.append(node.left)return result
defpost_order_traversal(root):# 后序"""
前序遍历为 root -> left -> right,后序遍历为 left -> right -> root,
可以修改前序遍历成为 root -> right -> left,那么这个顺序就和后序遍历正好相反
"""
result =[]
stack =[]
stack.append(root)whilelen(stack)!=0:
node = stack.pop()if node isNone:continue
result.append(node.val)
stack.append(node.left)
stack.append(node.right)return result[::-1]"""
5
/ \
3 6
/ \ /
2 4 7
"""defconstruct_tree():
root = TreeNode(5)
root.left = TreeNode(3)
root.right = TreeNode(6)
root.left.left = TreeNode(2)
root.left.right = TreeNode(4)
root.right.left = TreeNode(7)return root
if __name__ =='__main__':# from Tree.tree import construct_tree
root = construct_tree()print(hierarchical_traversal(root))print(in_order_traversal(root))print(pre_order_traversal(root))print(post_order_traversal(root))'''
print result
[5, 3, 6, 2, 4, 7]
[2, 3, 4, 5, 7, 6]
[5, 3, 2, 4, 6, 7]
[2, 4, 3, 7, 6, 5]
'''