算法题:

  • 链接:https://leetcode-cn.com/problems/same-tree/
  • 描述:
    • 给你两棵二叉树的根节点 p 和 q ,编写一个函数来检验这两棵树是否相同。
    • 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的。
  • 解题 ```javascript /**
    • @param {TreeNode} p
    • @param {TreeNode} q
    • @return {boolean} */ var isSameTree = function(p, q) { if(p===null && q===null) {
      1. return true
      } if (p===null || q===null) {
      1. return false
      } if (p.val !==q.val) {
      1. return false
      }
  1. return isSameTree(p.left,q.left) && isSameTree(p.right,q.right)

};

  1. <a name="LbhHM"></a>
  2. ### 手写题:
  3. - 链接:[https://bigfrontend.dev/zh/problem/virtual-dom-v-jsx-2](https://bigfrontend.dev/zh/problem/virtual-dom-v-jsx-2)
  4. - 解题:
  5. ```javascript
  6. function checkForClosing(str, i) {
  7. if (str[i] == "/") return true;
  8. if (str[i] == ">") return false;
  9. return checkForClosing(str, i + 1);
  10. }
  11. function parseChild(str) {
  12. console.log('str',str)
  13. console.log('===============')
  14. // str = str.trim();
  15. let child = [];
  16. let text = "";
  17. for (var i = 0; i < str.length; i++) {
  18. if (str[i] == "<" && checkForClosing(str, i)) {
  19. console.log('break')
  20. break;
  21. }
  22. if (str[i] == "<") {
  23. let newStr = str.slice(i);
  24. const result = parse(newStr);
  25. child.push(text);
  26. child.push(result);
  27. i += result.endClosing;
  28. text = "";
  29. continue;
  30. }
  31. else {
  32. text += str[i];
  33. }
  34. }
  35. console.log('outer')
  36. child.push(text);
  37. const nextTag = i;
  38. if (nextTag == 0) return [null, nextTag];
  39. return [child, nextTag];
  40. }
  41. function parseTag(str) {
  42. let isClosing = false;
  43. let closeOpening = str.indexOf(">");
  44. let tag = str.slice(1, closeOpening).trim();
  45. if (tag[0] == "/") {
  46. isClosing = true;
  47. tag = tag.slice(1).trim();
  48. }
  49. return [tag, closeOpening, isClosing]
  50. }
  51. function parse(code) {
  52. // your code here
  53. code = code.trim();
  54. if (code[0] !== "<" || code[code.length - 1] !== ">") {
  55. throw new Error("");
  56. }
  57. if (code.split("<").length !== code.split(">").length) {
  58. throw new Error("");
  59. }
  60. let i = 1;
  61. var [opening, endOpening, isClosing] = parseTag(code, i);
  62. code = code.substr(endOpening + 1, code.length);
  63. i += endOpening;
  64. var [child, endChild] = parseChild(code, i);
  65. code = code.substr(endChild, code.length);
  66. i += endChild;
  67. var [closing, endClosing, isClosing] = parseTag(code, i);
  68. code = code.substr(endClosing + 1, code.length);
  69. i += endClosing;
  70. if (!isClosing) throw new Error("Closing element not found");
  71. if (opening !== closing) {
  72. throw new Error("Opening and closing doesn't match")
  73. }
  74. let res = {
  75. opening,
  76. child: [child],
  77. closing,
  78. endClosing: i
  79. }
  80. return res;
  81. }
  82. /**
  83. * @param {any} your AST
  84. * @returns {string}
  85. */
  86. function generate(ast) {
  87. // your code here
  88. if (ast.child[0] == null) {
  89. return h(`${ast.opening}`, null);
  90. } else {
  91. let childrens = ast.child[0];
  92. let newChildrens = []
  93. for (let i = 0; i < childrens.length; i++) {
  94. if (childrens[i] instanceof Object) {
  95. newChildrens.push(generate(childrens[i]))
  96. } else {
  97. childrens[i] != "" && newChildrens.push(childrens[i]);
  98. }
  99. }
  100. let opening;
  101. if (`${ast.opening}`[0] == `${ast.opening}`[0].toUpperCase()) {
  102. opening = Heading;
  103. } else {
  104. opening = `${ast.opening}`;
  105. }
  106. return h(opening, null, ...newChildrens);
  107. }
  108. }
  • 自己没写出来,最后啃别人的代码啃懂了,还需要多练习几次。加强对源码的阅读和临摹,之前这是这块逻辑懂了,但是写起来太费劲了。整了一个下午