We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent b711825 commit 7c9f6fdCopy full SHA for 7c9f6fd
32. 二叉树的中序遍历.md
@@ -0,0 +1,27 @@
1
+***给定一个二叉树的根节点 root ,返回它的中序遍历 。***
2
+
3
+```
4
+# Definition for a binary tree node.
5
+# class TreeNode:
6
+# def __init__(self, val=0, left=None, right=None):
7
+# self.val = val
8
+# self.left = left
9
+# self.right = right
10
+class Solution:
11
+ def inorderTraversal(self, root: Optional[TreeNode]) -> List[int]:
12
+ #思路:栈,中序遍历:左,中,右,那么入栈顺序必须调整为倒序,也就是右,中,左
13
+ UNUSED, USED = 0, 1
14
+ stack=[(UNUSED, root)]
15
+ res = []
16
+ while stack:
17
+ state, node = stack.pop()
18
+ if not node:
19
+ continue
20
+ if state == 0:
21
+ stack.append((UNUSED, node.right))
22
+ stack.append((USED, node))
23
+ stack.append((UNUSED, node.left))
24
+ else:
25
+ res.append(node.val)
26
+ return res
27
0 commit comments