Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

js Binary Tree Generator / js 二叉树生成器 #143

Open
xgqfrms opened this issue Feb 19, 2023 · 0 comments
Open

js Binary Tree Generator / js 二叉树生成器 #143

xgqfrms opened this issue Feb 19, 2023 · 0 comments
Labels
Binary Tree Generator Binary Tree Generator 二叉树生成器 二叉树生成器

Comments

@xgqfrms
Copy link
Owner

xgqfrms commented Feb 19, 2023

js Binary Tree Generator / js 二叉树生成器

export {};


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

// LeetCode 二叉树生成器
BinaryTreeGenerator = (arr = [], i = 0) => {
  if(arr.length === 0 || i > arr.length - 1 || arr[i] === null) {
    return null;
  }
  const node = new TreeNode(arr[i]);
  // 先左后右, 依次遍历
  node.left = BinaryTreeGenerator(arr, 2 * i + 1);
  node.right = BinaryTreeGenerator(arr, 2 * i + 2);
  return node;
}


/*

BinaryTreeGenerator([3,9,20,null,null,15,7]);

https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

*/

https://assets.leetcode.com/uploads/2021/02/19/tree1.jpg

https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/

作者:xgqfrms
链接:https://www.cnblogs.com/xgqfrms/p/17081303.html
来源:https://www.cnblogs.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
©xgqfrms 2012-2023
www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!
原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️

@xgqfrms xgqfrms added Binary Tree Generator Binary Tree Generator 二叉树生成器 二叉树生成器 labels Feb 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Binary Tree Generator Binary Tree Generator 二叉树生成器 二叉树生成器
Projects
None yet
Development

No branches or pull requests

1 participant