📑 题目:94. 二叉树的中序遍历

🚀 本题 LeetCode 传送门

题目大意

中根遍历一颗树。

解题思路

递归的实现方法,见代码。

代码

  1. package leetcode
  2. /**
  3. * Definition for a binary tree node.
  4. * type TreeNode struct {
  5. * Val int
  6. * Left *TreeNode
  7. * Right *TreeNode
  8. * }
  9. */
  10. func inorderTraversal(root *TreeNode) []int {
  11. var result []int
  12. inorder(root, &result)
  13. return result
  14. }
  15. func inorder(root *TreeNode, output *[]int) {
  16. if root != nil {
  17. inorder(root.Left, output)
  18. *output = append(*output, root.Val)
  19. inorder(root.Right, output)
  20. }
  21. }