zoukankan      html  css  js  c++  java
  • [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"}
  • 相关阅读:
    idea14导入eclipse项目并部署运行完整步骤
    Java之Socket
    Docker之宿主机ssh至docker容器
    ElasticSearch的安装、使用、踩坑
    Linux下grep、tail、wc、awk文件处理命令
    Spring中@Async注解实现“方法”的异步调用
    Thrift——栗子
    Linux中的守护进程——supervise
    【composer】 PHP composer 镜像地址更换
    【Mac】解决macos安装升级时报错安装所选更新时发生错误的问题
  • 原文地址:https://www.cnblogs.com/Answer1215/p/10513146.html
Copyright © 2011-2022 走看看