-
Notifications
You must be signed in to change notification settings - Fork 0
/
inorderTraversal.py
56 lines (50 loc) · 1.16 KB
/
inorderTraversal.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
'''
Source : https://leetcode.com/problems/binary-tree-inorder-traversal/
Author : Yuan Wang
Date : 2018-07-30
/**********************************************************************************
*Given a binary tree, return the inorder traversal of its nodes' values.
*
*Example:
*
*Input: [1,null,2,3]
* 1
* \
* 2
* /
* 3
*
*Output: [1,3,2]
**********************************************************************************/
'''
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
#Recursive solution,Time complexity:O(n) Space complexity:O(n)
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
return self.printInorder(root,[])
def printInorder(self,root,lst):
if root:
self.printInorder(root.left,lst)
lst.append(root.val)
self.printInorder(root.right,lst)
return lst
#Iteratively solution, Time complexity:O(n) Space complexity:O(n)
def inorderTraversal(self,root):
res,stack=[],[]
while True:
while root:
stack.append(root)
root=root.left
if not stack:
return res
node=stack.pop()
res.append(node.val)
root=node.right