Leetcode 94.二叉树的中序遍历 (每日一题 20210712)
生活随笔
收集整理的這篇文章主要介紹了
Leetcode 94.二叉树的中序遍历 (每日一题 20210712)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
給定一個二叉樹的根節點 root ,返回它的 中序?遍歷。示例 1:輸入:root = [1,null,2,3]
輸出:[1,3,2]
示例 2:輸入:root = []
輸出:[]
示例 3:輸入:root = [1]
輸出:[1]
示例 4:輸入:root = [1,2]
輸出:[2,1]
示例 5:輸入:root = [1,null,2]
輸出:[1,2]鏈接:https://leetcode-cn.com/problems/binary-tree-inorder-traversalclass Solution:def inorderTraversal(self, root:TreeNode) -> List[int]:white, Gary = 0, 1res = []stack = [(white, root)]while stack != []:color, node = stack.pop()if node == None:continueif color == white:stack.append((white, node.right))stack.append((Gray, node))stack.append((white, node.left))else:stack.append(node.val)return res
總結
以上是生活随笔為你收集整理的Leetcode 94.二叉树的中序遍历 (每日一题 20210712)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Attention-OCR(Attent
- 下一篇: Leetcode 215.数组中第k个最