-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path之字形打印二叉树.py
32 lines (32 loc) · 939 Bytes
/
之字形打印二叉树.py
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
# -*- coding:utf-8 -*-
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def Print(self, pRoot):
# write code here
if not pRoot:
return []
node_stack = []
node_stack.append(pRoot)
res = []
while node_stack:
result = []
next_stack = []
for i in node_stack:
result.append(i.val)
if i.left:
next_stack.append(i.left)
if i.right:
next_stack.append(i.right)
node_stack = next_stack
res.append(result)
return_result = []
for index, value in enumerate(res):
if index%2 == 0:
return_result.append(value)
else:
return_result.append(value[::-1])
return return_result