[LeetCode][Python3] 94. Binary Tree Inorder Traversal
2019. 1. 3. 01:26 |
프로그래밍/LeetCode
Problem :
https://leetcode.com/problems/binary-tree-inorder-traversal/
My Solution :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ret = []
stack = []
curr = root
while stack or curr:
if curr:
stack.append(curr)
curr = curr.left
else:
curr = stack.pop()
ret.append(curr.val)
curr = curr.right
return ret
My Solution2 :
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None
class Solution:
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
ret = []
def inorder(node):
if node:
inorder(node.left)
ret.append(node.val)
inorder(node.right)
inorder(root)
return ret
'프로그래밍 > LeetCode' 카테고리의 다른 글
[LeetCode][Python3] 454. 4Sum II (1) | 2019.01.13 |
---|---|
[LeetCode][Python3] 78. Subsets (0) | 2019.01.13 |
[LeetCode][Python3] 238. Product of Array Except Self (0) | 2019.01.12 |
[LeetCode][Python3] 347. Top K Frequent Elements (0) | 2019.01.05 |
[LeetCode][Python3] 62. Unique Paths (6) | 2018.11.15 |
[LeetCode][Python3] 55. Jump Game (0) | 2018.11.15 |
[LeetCode][Python3] 49. Group Anagrams (0) | 2018.11.14 |
[LeetCode][Python3] 48. Rotate Image (0) | 2018.11.13 |
최근에 달린 댓글 최근에 달린 댓글