describe
Given the root of a binary tree, return the inorder traversal of its nodes’ values.
Example 1:
Input: root = [1,null,2,3]
Output: [1,3,2]
Copy the code
Example 2:
Input: root = []
Output: []
Copy the code
Example 3:
Input: root = [1]
Output: [1]
Copy the code
Example 4:
Input: root = [1,2]
Output: [2,1]
Copy the code
Example 5:
Input: root = [1, NULL,2] Output: [1,2]Copy the code
Note:
The number of nodes in the tree is in the range [0, 100].
-100 <= Node.val <= 100
Copy the code
parsing
A binary tree is traversed in middle order, in which all the values are added to the list of results in the order first left, then root, and then right using recursion.
answer
# Definition for a binary tree node.
# class TreeNode(object):
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution(object):
def inorderTraversal(self, root):
"""
:type root: TreeNode
:rtype: List[int]
"""
r = []
def inO(root):
if not root:
return
inO(root.left)
r.append(root.val)
inO(root.right)
inO(root)
return r
Copy the code
The results
Given the node in the Python online submission for Binary Tree Inorder Traversal. Given in Python online submissions for Binary Tree Inorder Traversal.Copy the code
Original link: leetcode.com/problems/bi…
Your support is my biggest motivation