it's possible to create a function auto generator this special test case binary tree from array in javascript?-编程思维

It's possible to create a function auto generator this special test case binary tree from array in javascript?

I want to auto generate those test cases in my local env. I'm not asking the LeetCode problem's answer, please read my question first!

situation

generator the special test case binary tree for local testing, which isn't a search binary tree;

I have an array of nodes of a binary tree and wanted to create a special binary tree like the below image shows.

// nodes array
const root = [3,9,20,null,null,15,7]

I tried a few things and searched google, still can't find a solution;
is there any way to find a solution to this problem, please?

PS: I know that I can manually create it.


// created by manually
const specialTestCaseTree = {
  val: 3,
  left: {
    val: 9,
    left: null,
    right: null,
  },
  right: {
    val: 20,
    left: {
      val: 15,
      left: null,
      right: null,
    },
    right: {
      val: 7,
      left: null,
      right: null,
    },
  },
};

try

class TreeNode {
  constructor(val, left, right) {
    this.val = (val === undefined ? 0 : val);
    this.left = (left === undefined ? null : left);
    this.right = (right === undefined ? null : right);
  }
}

class BinaryTreeGenerator {
  constructor() {
    this.root = null;
    this.flag = `left`;
  }
  insert(key) {
    if(this.root === null) {
      this.root = new TreeNode(key);
    } else {
      this.insertNode(this.root, key)
    }
  }
  insertNode(node, key) {
    // ignore null
    if(key === null) {
      return;
    }
    // tree traversal(root=>left=>right)
    if(this.flag === `left`) {
      // left
      if(node.left === null) {
        node.left = new TreeNode(key);
        this.flag = `right`;
      } else {
        this.insertNode(node.left, key)
      }
    } else {
      // right
      if(node.right === null) {
        node.right = new TreeNode(key);
        this.flag = `left`;
      } else {
        this.insertNode(node.right, key)
      }
    }
  }
}

// testing

const tree = new BinaryTreeGenerator();

const arr = [3,9,20,null,null,15,7];

for (let key of arr) {
  tree.insert(key)
}

console.log(`tree =`, tree);
// ❌ not the wanted

references

https://leetcode.com/problems/maximum-depth-of-binary-tree/description/

Fuck 小日本

LeetCode & tree

  1. Binary Tree Paths / 257. 二叉树路径

https://leetcode.com/problems/binary-tree-paths/description/

  1. Flip Equivalent Binary Trees / 951. 翻转等价二叉树

https://leetcode.com/problems/flip-equivalent-binary-trees/

(🐞 反爬虫测试!打击盗版⚠️)如果你看到这个信息, 说明这是一篇剽窃的文章,请访问 https://www.cnblogs.com/xgqfrms/ 查看原创文章!

refs

https://stackoverflow.com/questions/75295344/its-possible-to-create-a-function-auto-generator-this-special-test-case-binary

https://stackoverflow.com/questions/73202858/test-leetcode-104-maximum-depth-of-binary-tree

https://stackoverflow.com/questions/48744012/how-to-make-binary-tree-from-array-in-javascript

https://stackoverflow.com/questions/60175433/flip-equivalent-binary-trees-in-leetcode



©xgqfrms 2012-2021

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


版权声明:本文版权归作者所有,遵循 CC 4.0 BY-SA 许可协议, 转载请注明原文链接
https://www.cnblogs.com/xgqfrms/p/17080136.html

「tricks」平凡二分幻术-编程思维

其实这个的标题叫 平凡线段树上二分幻术,因为这是一个民科在乱叫。 如标题所言,这个东西确实非常 trivial。碍于网络上没有一个成体系的文章供参考就只能自己来炒炒冷饭。 如果出了什么 bug 就当个笑话看。 我们这样来描述一类问题 给出一个序列 \(\{a_n\}\) 以及函数 \(\textbf{f}(x)\i