【问题标题】:console.log Nodes in a Binary Tree who’s subtrees have Even sumconsole.log 二叉树中的节点,其子树的总和为偶数
【发布时间】:2021-12-18 02:11:06
【问题描述】:

以下是我正在处理的编码挑战。

给你一棵二叉树,其中每个节点都包含一个值。 设计一个算法来打印其子树加起来的所有节点 偶数。

这是我用于测试的树以及我的函数:

class Node {
  constructor(val) {
    this.val = val;
    this.left = null;
    this.right = null;
  }
}

const a = new Node(3);
const b = new Node(11);
const c = new Node(4);
const d = new Node(4);
const e = new Node(-2);
const f = new Node(2);

a.left = b;
a.right = c;
b.left = d;
b.right = e;
c.right = f;

//       3
//    /    \
//   11     4
//  / \      \
// 4   -2     2

const isEven = (node) => {
  if (node === null) return 0;
  let left = isEven(node.left);
  let right = isEven(node.right);
  let sum = left + right + node.val;
  if (sum % 2 === 0) {
    console.log(node.val);
  }
  return;
};

console.log(isEven(a));

该功能没有按我想要的方式工作。

鉴于这棵树,我认为正确的输出应该是:3、4、-2、4 aka a、d、e 和 c。 (假设 null = 0) 但我得到的输出是:4, -2, 2, undefined

我不确定 2 是从哪里来的,因为没有节点等于 2。(这是我的错误)

【问题讨论】:

    标签: javascript algorithm data-structures tree binary


    【解决方案1】:

    您可以使函数返回子树总和。然后,将左右孩子的函数调用结果与节点本身的值相加,得到以该节点为根的子树的总和。

    class Node {
      constructor(val) {
        this.val = val;
        this.left = null;
        this.right = null;
      }
    }
    
    const a = new Node(3);
    const b = new Node(11);
    const c = new Node(4);
    const d = new Node(4);
    const e = new Node(-2);
    const f = new Node(2);
    
    a.left = b;
    a.right = c;
    b.left = d;
    b.right = e;
    c.right = f;
    
    const checkEven = node => {
      if(!node) return 0;
      const sum = node.val + checkEven(node.left) + checkEven(node.right);
      if(sum % 2 === 0) console.log(node.val);
      return sum;
    }
    checkEven(a);

    【讨论】:

      【解决方案2】:

      我得到的输出是:4, -2, 2, undefined。

      最后得到undefined 的原因是,您执行了主isEven 调用的console.log,但您的isEven 函数没有返回任何内容:它的最后一条指令是return,所以主要console.log 输出undefined。实际上,您不应该在主程序中执行console.log,因为节点的打印已经在您的函数中进行了处理。

      我不确定 2 是从哪里来的,因为没有节点等于 2。

      节点f 的值为2,它应该在输出中。

      我认为正确的输出应该是:3、4、-2、4 aka a、d、e 和 c。 (假设 null = 0)

      ...和f

      您没有得到所有结果,因为isEven 只能返回nullundefined,因此left + right 不会给出您所期望的结果:将null 添加到数字将视为null为 0,但当涉及 undefined 时,表达式将计算为 NaN

      这可以通过将最终的 return 更改为 return sum 来解决。

      以下是您的脚本已通过这两个更正进行了更正:

      class Node {
        constructor(val) {
          this.val = val;
          this.left = null;
          this.right = null;
        }
      }
      
      const a = new Node(3);
      const b = new Node(11);
      const c = new Node(4);
      const d = new Node(4);
      const e = new Node(-2);
      const f = new Node(2);
      
      a.left = b;
      a.right = c;
      b.left = d;
      b.right = e;
      c.right = f;
      
      //       3
      //    /    \
      //   11     4
      //  / \      \
      // 4   -2     2
      
      const isEven = (node) => {
        if (node === null) return 0;
        let left = isEven(node.left);
        let right = isEven(node.right);
        let sum = left + right + node.val;
        if (sum % 2 === 0) {
          console.log(node.val);
        }
        return sum; // <-- sum!
      };
      
      isEven(a); // <-- no console.log

      替代树构造

      与您的问题无关,但您可以通过定义用于指定 leftright 引用的参数来使您的 Node 构造函数更加灵活。然后,您可以在一个嵌套表达式中构建树。

      class Node {
        constructor(val, left=null, right=null) {  // <-- extra parameters
          this.val = val;
          this.left = left;
          this.right = right;
        }
      }
      
      const a = new Node(3,
          new Node(11,
              new Node(4),
              new Node(-2)
          ),
          new Node(4,
              null,
              new Node(2)
          )
      );
      
      const isEven = (node) => {
        if (node === null) return 0;
        let left = isEven(node.left);
        let right = isEven(node.right);
        let sum = left + right + node.val;
        if (sum % 2 === 0) {
          console.log(node.val);
        }
        return sum;
      };
      
      isEven(a);

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-05
        • 1970-01-01
        • 2016-04-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-24
        相关资源
        最近更新 更多