[Algorithm] Given the root to a binary tree, return the deepest node

By given a binary tree, and a root node, find the deepest node of this tree.

 

We have way to create node:

复制代码
function createNode(val, left = null, right = null) {
  return {
    val,
    left,
    addLeft(leftKey) {
      return (this.left = leftKey ? createNode(leftKey) : null);
    },
    right,
    addRight(rightKey) {
      return (this.right = rightKey ? createNode(rightKey) : null);
    }
  };
}
复制代码

 

Way to create tree:

复制代码
function createBT(rootKey) {
  const root = createNode(rootKey);
  return {
    root,
    deepest(node) {
      // code goes here
    }
  };
}
复制代码

 

Way to construct tree:

复制代码
const tree = createBT("root");
const root = tree.root;
const left = root.addLeft("left");
root.addRight("right");

const leftleft = left.addLeft("left.left");
const leftleftleft = leftleft.addLeft("left.left.left");
const leftright = left.addRight("left.right");
leftright.addLeft("left.right.left");
复制代码

 

The way to solve the problem is recursive calling the 'deepest' function for node's left and right leaf, until we reach the base case, which is the node that doesn't contian any left or right leaf.

复制代码
function createNode(val, left = null, right = null) {
  return {
    val,
    left,
    addLeft(leftKey) {
      return (this.left = leftKey ? createNode(leftKey) : null);
    },
    right,
    addRight(rightKey) {
      return (this.right = rightKey ? createNode(rightKey) : null);
    }
  };
}

function createBT(rootKey) {
  const root = createNode(rootKey);
  return {
    root,
    deepest(node) {
      function helper(node, depth) {
        if (node && !node.left && !node.right) {
          return {
            depth,
            node
          };
        }

        if (node.left) {
          return helper(node.left, depth + 1);
        } else if (node.right) {
          return helper(node.right, depth + 1);
        }
      }

      const { depth: ld, node: ln } = helper(root.left, 1);
      const { depth: rd, node: rn } = helper(root.right, 1);

      const max = Math.max(ld, rd);
      if (max === ld) {
        return { depth: ld, node: ln.val };
      } else {
        return { depth: rd, node: rn.val };
      }
    }
  };
}

const tree = createBT("root");
const root = tree.root;
const left = root.addLeft("left");
root.addRight("right");

const leftleft = left.addLeft("left.left");
const leftleftleft = leftleft.addLeft("left.left.left");
const leftright = left.addRight("left.right");
leftright.addLeft("left.right.left");

console.log(tree.deepest(root)); // {depth: 3, node: "left.left.left"}
复制代码

 

posted @   Zhentiw  阅读(388)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具
历史上的今天:
2018-03-11 [HTML5] Text Alternatives
2018-03-11 [HTML5] Semantics for accessibility
2016-03-11 [RxJS] Logging a Stream with do()
2016-03-11 [RxJS] Handling a Complete Stream with Reduce
2016-03-11 [RxJS] Completing a Stream with TakeWhile
2016-03-11 [RxJS] Adding Conditional Logic with Filter
2016-03-11 [RxJS] Combining Streams with CombineLatest
点击右上角即可分享
微信分享提示