forked from EINDEX/Python-algorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary_tree.py
More file actions
40 lines (29 loc) · 801 Bytes
/
Copy pathbinary_tree.py
File metadata and controls
40 lines (29 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
from data_structure.tree.tree import Tree, TreeNode
class BinaryTreeNode(TreeNode):
"""
一个二叉树节点
"""
def __init__(self, value, left, right):
self.left = left
self.right = right
super().__init__(value, [left, right])
class BinaryTree(Tree):
"""
二叉树:基本二叉树的数据结构
"""
def inorder_traversal_while(self):
"""
二叉树的中序遍历
:return: list of node values
"""
res = []
if not self.root:
return res
stack = [self.root]
node = self.root.left
while len(stack):
while node:
stack.append(node)
node = node.left
res.append(stack[-1].value)
return res