标签:val img tco roo ack treenode alt 迭代 inf
题目描述:
方法一:迭代
class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: res,stack = [],[] while True: while root: stack.append(root) root = root.left if not stack: return res node = stack.pop() res.append(node.val) root = node.right return res
方法二:递归
class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: res = [] def helper(root): if not root: return helper(root.left) res.append(root.val) helper(root.right) helper(root) return res
另;
class Solution: def inorderTraversal(self, root: TreeNode) -> List[int]: f = self.inorderTraversal return f(root.left) + [root.val] + f(root.right) if root else []
标签:val img tco roo ack treenode alt 迭代 inf
原文地址:https://www.cnblogs.com/oldby/p/11181674.html