Lunski's Clutter

This is a place to put my clutters, no matter you like it or not, welcome here.

0%

94. Binary Tree Inorder Traversal

Given the root of a binary tree, return the inorder traversal of its nodes’ values.

Example 1:

1
2
Input: root = [1,null,2,3]
Output: [1,3,2]

Example 2:

1
2
Input: root = []
Output: []

Example 3:

1
2
Input: root = [1]
Output: [1]

Example 4:

1
2
Input: root = [1,2]
Output: [2,1]

Example 5:

1
2
Input: root = [1,null,2]
Output: [1,2]

二元樹尋訪應該是樹類最基本題目了,尋訪分三種,前序,中序,後序,依照值(root.val)位置決定是哪種。

1
2
3
preorder: 中->左->右  
inorder: 左->中->右
postorder: 左->右->中

而題目需要中序,所以可以寫成

1
2
3
4
5
def inOrder(root): 
if root != None:
inOrder(root.left)
print(root.val,end=" ")
inOrder(root.right)

如果你覺得這篇文章很棒,請你不吝點讚 (゚∀゚)

Welcome to my other publishing channels