{
  "type": "code_options",
  "author": "https://github.com/qiyuangong/leetcode",
  "source": "solution.md",
  "exercise_id": "1f488d8bd1d2415e8e24ede9f742f962",
  "keywords": "栈,树,深度优先搜索,二叉树",
  "title": "二叉树的中序遍历",
  "desc": [
    {
      "content": "\n<p>给定一个二叉树的根节点 <code>root</code> ，返回它的 <strong>中序</strong> 遍历。</p><p> </p><p><strong>示例 1：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_1.jpg\" style=\"width: 202px; height: 324px;\" /><pre><strong>输入：</strong>root = [1,null,2,3]<strong><br />输出：</strong>[1,3,2]</pre><p><strong>示例 2：</strong></p><pre><strong>输入：</strong>root = []<strong><br />输出：</strong>[]</pre><p><strong>示例 3：</strong></p><pre><strong>输入：</strong>root = [1]<strong><br />输出：</strong>[1]</pre><p><strong>示例 4：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_5.jpg\" style=\"width: 202px; height: 202px;\" /><pre><strong>输入：</strong>root = [1,2]<strong><br />输出：</strong>[2,1]</pre><p><strong>示例 5：</strong></p><img alt=\"\" src=\"https://cdn.jsdelivr.net/gh/doocs/leetcode@main/solution/0000-0099/0094.Binary%20Tree%20Inorder%20Traversal/images/inorder_4.jpg\" style=\"width: 202px; height: 202px;\" /><pre><strong>输入：</strong>root = [1,null,2]<strong><br />输出：</strong>[1,2]</pre><p> </p><p><strong>提示：</strong></p><ul>\t<li>树中节点数目在范围 <code>[0, 100]</code> 内</li>\t<li><code>-100 <= Node.val <= 100</code></li></ul><p> </p><p><strong>进阶:</strong> 递归算法很简单，你可以通过迭代算法完成吗？</p>",
      "language": "markdown"
    }
  ],
  "answer": [
    {
      "content": "",
      "language": "python"
    }
  ],
  "prepared": [
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ],
    [
      {
        "content": "",
        "language": "python"
      }
    ]
  ],
  "template": {
    "content": "class TreeNode(object):\n\tdef __init__(self, x):\n\t\tself.val = x\n\t\tself.left = None\n\t\tself.right = None\nclass List2Tree(object):\n\tdef __init__(self, nums: list):\n\t\tself.nums = nums\n\t\tself.queue = []\n\t\tif len(nums) == 1:\n\t\t\tself.root = TreeNode(self.nums.pop(0))\n\t\telse:\n\t\t\ta = self.nums.pop(0)\n\t\t\tb = self.nums.pop(0)\n\t\t\tc = self.nums.pop(0)\n\t\t\tself.root = TreeNode(a)\n\t\t\tif b is not None:\n\t\t\t\tself.root.left = TreeNode(b)\n\t\t\telse:\n\t\t\t\tself.root.left = b\n\t\t\tif c is not None:\n\t\t\t\tself.root.right = TreeNode(c)\n\t\t\telse:\n\t\t\t\tself.root.right = c\n\t\t\tself.queue.append(self.root.left)\n\t\t\tself.queue.append(self.root.right)\n\tdef convert(self):\n\t\twhile len(self.nums) > 0 and len(self.queue)> 0:\n\t\t\tnode = self.queue.pop(0)\n\t\t\tif node is not None:\n\t\t\t\tnum= self.nums.pop(0)\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.left = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.left = num\n\t\t\t\tif len(self.nums) > 0:\n\t\t\t\t\tnum = self.nums.pop(0)\n\t\t\t\telse:\n\t\t\t\t\tnum = None\n\t\t\t\tif num is not None:\n\t\t\t\t\tnode.right = TreeNode(num)\n\t\t\t\telse:\n\t\t\t\t\tnode.right = num\n\t\t\t\tself.queue.append(node.left)\n\t\t\t\tself.queue.append(node.right)\n\t\treturn self.root\nclass Solution(object):\n\tdef inorderTraversal(self, root):\n\t\tif root is None:\n\t\t\treturn []\n\t\troot = List2Tree(root).convert()\n\t\tres = []\n\t\tstack = [root]\n\t\twhile len(stack) > 0:\n\t\t\tcurr = stack.pop()\n\t\t\tif not isinstance(curr, TreeNode):\n\t\t\t\tres.append(curr)\n\t\t\t\tcontinue\n\t\t\tif curr.right is not None:\n\t\t\t\tstack.append(curr.right)\n\t\t\tstack.append(curr.val)\n\t\t\tif curr.left is not None:\n\t\t\t\tstack.append(curr.left)\n\t\treturn res\n# %%\ns = Solution()\nprint(s.inorderTraversal(root = [1,None,2,3]))",
    "language": "python"
  },
  "node_id": "dailycode-8d314c28e1e44f6e8b8c85d1ddc064fb",
  "license": "csdn.net",
  "created_at": 1637894161,
  "topic_link": "https://bbs.csdn.net/topics/600471010"
}