[关闭]
@Lin-- 2020-02-24T14:00:35.000000Z 字数 518 阅读 288

inorderTravesal

Leetcode


中序遍历二叉树,把输出改成添加数组值即可

  1. '''
  2. # File: inorderTraveral.py
  3. # Author: 0HP
  4. # Date: 20200223
  5. # Purpose: solve the problem in website:
  6. # https://leetcode.com/problems/binary-tree-inorder-traversal/
  7. '''
  8. # Definition for a binary tree node.
  9. class TreeNode:
  10. def __init__(self, x):
  11. self.val = x
  12. self.left = None
  13. self.right = None
  14. class Solution:
  15. def InOrder(self,root:TreeNode,l:list):
  16. if root==None:
  17. return
  18. self.InOrder(root.left,l)
  19. l.append(root.val)
  20. self.InOrder(root.right,l)
  21. def inorderTraversal(self, root: TreeNode) -> List[int]:
  22. a=[]
  23. self.InOrder(root,a)
  24. return a
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注