Skip to content

Commit 032bf43

Browse files
author
wb.xubilin
committed
二叉树 两道题 104 110
1 parent f21f2aa commit 032bf43

File tree

4 files changed

+87
-1
lines changed

4 files changed

+87
-1
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from initTree import init_tree_from_list
2+
def bfs(node,op_func):
3+
queue = [node]
4+
5+
while len(queue) != 0:
6+
op_node = queue[0]
7+
queue.remove(op_node)
8+
op_func(op_node)
9+
if op_node.left_child is not None:
10+
queue.append(op_node.left_child)
11+
if op_node.right_child is not None:
12+
queue.append(op_node.right_child)
13+
14+
15+
if __name__ == "__main__":
16+
test_list = range(10)
17+
base_node = init_tree_from_list(test_list)
18+
def visit(node):
19+
print node.value
20+
21+
bfs(base_node,visit)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
from initTree import init_tree_from_list
2+
def dfs_recursion(node,op_func):
3+
op_func(node)
4+
if node.left_child != None:
5+
dfs_recursion(node.left_child,op_func)
6+
if node.right_child != None:
7+
dfs_recursion(node.right_child,op_func)
8+
9+
def dfs_division(node):
10+
ret = [node.value]
11+
right = []
12+
left = []
13+
if node.left_child != None:
14+
left = dfs_division(node.left_child)
15+
16+
if node.right_child != None:
17+
right = dfs_division(node.right_child)
18+
19+
ret += left
20+
ret += right
21+
return ret
22+
23+
if __name__ == "__main__":
24+
test_list = range(10)
25+
base_node = init_tree_from_list(test_list)
26+
def visit(node):
27+
print node.value
28+
29+
print dfs_division(base_node)
30+
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from initTree import init_tree_from_list
2+
def isBalanced(root):
3+
"""
4+
:type root: TreeNode
5+
:rtype: bool
6+
"""
7+
flag = True
8+
if root is None:
9+
return flag
10+
11+
def division(node, depth, flag):
12+
if flag is False:
13+
return 0, False
14+
left_flag = True
15+
right_flag = True
16+
depth += 1
17+
left_depth = depth
18+
right_depth = depth
19+
if node.left_child is not None:
20+
left_depth, left_flag = division(node.left_child, depth, flag)
21+
if node.right_child is not None:
22+
right_depth, right_flag = division(node.right_child, depth, flag)
23+
if abs(right_depth - left_depth) > 1:
24+
flag = False
25+
else:
26+
flag = left_flag and right_flag
27+
return max(depth,left_depth, right_depth), flag
28+
29+
max_depth, flag = division(root, 0, flag)
30+
return flag
31+
32+
if __name__ == "__main__":
33+
test_list = [1,2,3,4,5,6,None,8]
34+
root = init_tree_from_list(test_list)
35+
print isBalanced(root)

new_issue/data_sturct/binary_tree/preorderTraverse.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from binaryTree import binaryTreeNode
1+
22
from initTree import init_tree_from_list
33

44
def preorder_traverse(node,op_func):

0 commit comments

Comments
 (0)