Skip to content

Commit b18a342

Browse files
committed
Binary Tree Inorder Traversal
1 parent 41df0fa commit b18a342

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

Binary_Tree_Inorder_Traversal.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Given a binary tree, return the inorder traversal of its nodes' values.
2+
#
3+
# Example:
4+
#
5+
# Input: [1,null,2,3]
6+
# 1
7+
# \
8+
# 2
9+
# /
10+
# 3
11+
#
12+
# Output: [1,3,2]
13+
# Definition for a binary tree node.
14+
15+
16+
class TreeNode:
17+
def __init__(self, x):
18+
self.val = x
19+
self.left = None
20+
self.right = None
21+
22+
23+
class Solution:
24+
def inorderTraversal(self, root):
25+
26+
stack = []
27+
res = []
28+
29+
while stack or root:
30+
while root:
31+
stack.append(root)
32+
root = root.left
33+
34+
root = stack.pop()
35+
36+
res.append(root.val)
37+
38+
root = root.right
39+
40+
return res

0 commit comments

Comments
 (0)