class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
return [root.val] + self.preorderTraversal(root.left) + self.preorderTraversal(root.right) if root else []
TODO
class Solution(object):
def preorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
return self.preorderTraversal(root.left) + [root.val] + self.preorderTraversal(root.right) if root else []
TODO
给定一个 N 叉树,返回其节点值的前序遍历。
例如,给定一个 3叉树
:
返回其前序遍历: [1,3,5,6,2,4]
。
"""
# Definition for a Node.
class Node(object):
def __init__(self, val=None, children=None):
self.val = val
self.children = children
"""
class Solution(object):
def preorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
if root == None:return []
res = [root.val]
for node in root.children:
res.extend(self.preorder(node))
return res
TODO
class Solution(object):
def postorder(self, root):
"""
:type root: Node
:rtype: List[int]
"""
if root == None:return []
res = []
for node in root.children:
res.extend(self.postorder(node))
res.append(root.val)
return res
TODO