forked from tcandzq/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinaryTreeInorderTraversal.py
More file actions
58 lines (47 loc) · 1.08 KB
/
BinaryTreeInorderTraversal.py
File metadata and controls
58 lines (47 loc) · 1.08 KB
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/8/8 22:11
# @Author : tc
# @File : BinaryTreeInorderTraversal.py
"""
给定一个二叉树,返回它的中序遍历。
Input:[1,null,2,3]
1
\
2
/
3
Output:[1,3,2]
本题需要用递归和非递归两种解法
递归解法:说白了,就是先左,然后中, 最后右
"""
class TreeNode:
def __init__(self,x):
self.val = x
self.left = None
self.right = None
#解法1:递归
def inorderTraversal(root):
node_list = []
helper(root,node_list)
return node_list
def helper(root,node_list):
if root:
if root.left:
helper(root.left,node_list)
node_list.append(root.val)
if root.right:
helper(root.right,node_list)
#解法2:
if __name__ == '__main__':
root = TreeNode(1)
node2 = TreeNode(2)
node3 = TreeNode(3)
node4 = TreeNode(4)
node5 = TreeNode(5)
root.left = node2
root.right = node3
node3.left = node4
node4.right = node5
for num in inorderTraversal(root):
print(num)