-
Notifications
You must be signed in to change notification settings - Fork 319
/
Copy path94_BinaryTreeInorderTraversal.py
48 lines (40 loc) · 1.13 KB
/
94_BinaryTreeInorderTraversal.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
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution(object):
# iteratively
def inorderTraversal(self, root):
tree_stack = []
inorder_tra = []
while root or tree_stack:
# Go along the left child
if root:
tree_stack.append(root)
root = root.left
# Meet a left, go back to the parent node
else:
node = tree_stack.pop()
inorder_tra.append(node.val)
root = node.right
return inorder_tra
class Solution_2(object):
# recursively
def inorderTraversal(self, root):
inorder_tra = []
self.helper(root, inorder_tra)
return inorder_tra
def helper(self, root, inorder_tra):
if root:
self.helper(root.left, inorder_tra)
inorder_tra.append(root.val)
self.helper(root.right, inorder_tra)
"""
[]
[1]
[1,2,3,null,null,4,null,null,5]
"""