Skip to content

Commit

Permalink
先序遍历和后序遍历
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Dec 2, 2018
1 parent da4f03e commit ec009f7
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
24 changes: 24 additions & 0 deletions lc144. Binary Tree Preorder Traversal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
def dfs(root):
if not root:
return
res.append(root.val)
dfs(root.left)
dfs(root.right)

res=[]
dfs(root)
return res

24 changes: 24 additions & 0 deletions lc145. Binary Tree Postorder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, x):
# self.val = x
# self.left = None
# self.right = None

class Solution:
def postorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
def dfs(root):
if not root:
return
dfs(root.left)
dfs(root.right)
res.append(root.val)

res=[]
dfs(root)
return res

0 comments on commit ec009f7

Please sign in to comment.