alex-trail-ELg86jOmFcc-unsplash.jpg
二叉树

代码

  1. /**
  2. * Definition for a binary tree node.
  3. * function TreeNode(val, left, right) {
  4. * this.val = (val===undefined ? 0 : val)
  5. * this.left = (left===undefined ? null : left)
  6. * this.right = (right===undefined ? null : right)
  7. * }
  8. */
  9. /**
  10. * @param {TreeNode} root
  11. * @return {number[]}
  12. */
  13. const inOrder = (root, res) => {
  14. if (root === null) {
  15. return null;
  16. }
  17. inOrder(root.left, res);
  18. res.push(root.val);
  19. inOrder(root.right, res);
  20. }
  21. var inorderTraversal = function (root) {
  22. const res = []
  23. inOrder(root, res);
  24. return res;
  25. };