1. class TreeNode {
  2. constructor(val) {
  3. this.value = val;
  4. this.left = null;
  5. this.right = null;
  6. }
  7. }
  8. // 二叉搜索树插入值
  9. function insertIntoBST(root, val) {
  10. if (root == NULL) {
  11. let node = new TreeNode(val);
  12. return node;
  13. }
  14. if (root.val > val) root.left = insertIntoBST(root.left, val);
  15. if (root.val < val) root.right = insertIntoBST(root.right, val);
  16. return root;
  17. }
  18. // 记录父节点
  19. function solution(root, val) {
  20. let parent;
  21. function traversal(cur, val) {
  22. if (cur === null) {
  23. let node = new TreeNode(val);
  24. if (parent.val > val) {
  25. parent.left = node;
  26. } else {
  27. parent.right = node;
  28. }
  29. return;
  30. }
  31. parent = cur;
  32. if (cur.val > val) traversal(cur.left, val);
  33. if (cur.val < val) traversal(cur.right, val);
  34. return;
  35. }
  36. function insertIntoBST(root, val) {
  37. parent = new TreeNode(0);
  38. if (root == NULL) {
  39. root = new TreeNode(val);
  40. }
  41. traversal(root, val);
  42. return root;
  43. }
  44. insertIntoBST(root, val)
  45. }
  46. // 迭代法
  47. function insertIntoBST(root, val) {
  48. if (root == NULL) {
  49. let node = new TreeNode(val);
  50. return node;
  51. }
  52. let cur = root;
  53. let parent = root;
  54. while (cur != NULL) {
  55. parent = cur;
  56. if (cur.val > val) cur = cur.left;
  57. else cur = cur.right;
  58. }
  59. node = new TreeNode(val);
  60. if (val < parent.val) parent.left = node;
  61. else parent.right = node;
  62. return root;
  63. }
  64. // 修剪✂️二叉搜索树
  65. function trimBST(root, low, high) {
  66. if (root == null) return null;
  67. if (root.val < low) return trimBST(root.right, low, high);
  68. if (root.val > high) return trimBST(root.left, low, high);
  69. root.left = trimBST(root.left, low, high);
  70. root.right = trimBST(root.right, low, high);
  71. return root;
  72. }
  73. // 迭代法修剪✂️二叉搜索树
  74. function trimBST(root, low, high) {
  75. if (!root) return null;
  76. while (root.value < low || root.value > high) {
  77. if (root.value < low) {
  78. root = root.right
  79. } else {
  80. root = root.left
  81. }
  82. }
  83. let cur = root;
  84. while (cur.left.value < low) {
  85. cur.left = cur.left.right;
  86. }
  87. cur = root;
  88. while (cur.right.value > high) {
  89. cur.right = cur.right.left;
  90. }
  91. return root;
  92. }
  93. // 把搜索树转换为累加树
  94. function traversal(cur) { // 右中左遍历
  95. if (cur == NULL) return;
  96. traversal(cur.right);
  97. cur.val += pre;
  98. pre = cur.val;
  99. traversal(cur.left);
  100. }
  101. function convertBST(root) {
  102. pre = 0;
  103. traversal(root);
  104. return root;
  105. }

后序遍历

image.png

前序遍历

image.png

中序遍历

image.png

迭代法中序遍历

  1. function Node(val) {
  2. this.left = null;
  3. this.right = null;
  4. this.val = val;
  5. }
  6. const inorderTraversal = (root) => {
  7. let result = [];
  8. let st = [];
  9. if (root !== null) st.push(root);
  10. while (st.length > 0) {
  11. let node = st[st.length - 1];
  12. if (node !== null) {
  13. st.pop();
  14. if (node.right) st.push(node.right);
  15. st.push(node);
  16. st.push(null);
  17. if (node.left) st.push(node.left);
  18. } else {
  19. st.pop();
  20. node = st[st.length - 1];
  21. result.push(node.val)
  22. }
  23. }
  24. return result;
  25. }