js特定深度节点链表

题目是给定一棵二叉树,设计一个算法,创建含有某一深度上所有节点的链表(比如,若一棵树的深度为 D,则会创建出 D 个链表)。返回一个包含所有深度的链表的数组。

解决这个问题,首先先写一个二叉树的对象,题目中给了


  Definition for a binary tree node.
  function TreeNode(val) {
      this.val = val;
      this.left = this.right = null;
  }
 Definition for singly-linked list.
 function ListNode(val!=undefind) {
     this.val = val;
     this.next = null;
 }

我想了一下,如果想要遍历二叉树,还想知道深度是多少,我能想到最简单的方法就是加个index 然后判断index是几 深度就是几


  Definition for a binary tree node.
  function TreeNode(val) {
      this.val = val;
      this.index = null
      this.left = this.right = null;
  }
 Definition for singly-linked list.
 function ListNode(val!=undefind) {
     this.val = val;
     this.next = null;
 }

设置好后,我们添加一个方法来遍历传入的树的值

 TreeNode.prototype.toString=function(o,index){
     this.index = index
     o[this.index] = o[this.index]==undefined?undefined:o[this.index]
     o[this.index+1] = o[this.index+1]==undefined?undefined:o[this.index+1]
     if(this.right!=null){
        this.right.toString(o,index+1)
     }
     if(this.left!=null){
         this.left.toString(o,index+1)
     }
     if(o[this.index]!=undefined){
         let oee = new ListNode(this.val)
         oee.next = o[this.index]
         o[this.index]= oee
     }else{
         o[this.index] = new ListNode(this.val)
     }
 }

这里我对深度设置为首次传入的为0 根据进入left 或者right的次数累加,o是用来收集我们每个不同深度的树新生成的对象的

总体代码为下

/**
			 * Definition for a binary tree node.
			 * function TreeNode(val) {
			 *     this.val = val;
			 *     this.index = 0 
			 *     this.left = this.right = null;
			 * }
			 */
			/**
			 * Definition for singly-linked list.
			 * function ListNode(val!=undefind) {
			 *     this.val = val;
			 *     this.next = null;
			 * }
			 */
			/**
			 * @param {TreeNode} tree
			 * @return {ListNode[]}
			 */
			 TreeNode.prototype.toString=function(o,index){
			     this.index = index
			     o[this.index] = o[this.index]==undefined?undefined:o[this.index]
			     o[this.index+1] = o[this.index+1]==undefined?undefined:o[this.index+1]
			     if(this.right!=null){
			        this.right.toString(o,index+1)
			     }
			     if(this.left!=null){
			         this.left.toString(o,index+1)
			     }
			     if(o[this.index]!=undefined){
			         let oee = new ListNode(this.val)
			         oee.next = o[this.index]
			         o[this.index]= oee
			     }else{
			         o[this.index] = new ListNode(this.val)
			     }
			 }
			var listOfDepth = function(tree) {
			    let p = []
			    tree.toString(p,0)
			    p=p.filter(val=>val!=undefined)
			    return p
			};

在这里插入图片描述
只能说效率一般,如果各位大佬有更好的办法,请给予指正,谢谢各位

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

自信小老头

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值