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 + );
} else if (node.right) {
return helper(node.right, depth + );
}
} const { depth: ld, node: ln } = helper(root.left, );
const { depth: rd, node: rn } = helper(root.right, ); 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"}

[Algorithm] Given the root to a binary tree, return the deepest node的更多相关文章

  1. Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / \ 9 20 / \

    class Solution { public: vector<vector<int>> levelOrder(TreeNode* root) { vector<vect ...

  2. [LintCode] Flatten Binary Tree to Linked List 将二叉树展开成链表

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

  3. [LintCode] Binary Tree Paths 二叉树路径

    Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 /   \2 ...

  4. LintCode Binary Tree Paths

    Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...

  5. Lintcode 175 Invert Binary Tree

    I did it in a recursive way. There is another iterative way to do it. I will come back at it later. ...

  6. Flatten Binary Tree to Linked List

    Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...

  7. lintcode :Invert Binary Tree 翻转二叉树

    题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...

  8. [Swift]LeetCode814. 二叉树剪枝 | Binary Tree Pruning

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

  9. [LeetCode] Binary Tree Pruning 二叉树修剪

    We are given the head node root of a binary tree, where additionally every node's value is either a ...

随机推荐

  1. 01-学前入门VS各个组成部分

    1)快捷打开运行窗口(Windows+R) 里面输入devenv命令快捷打开VS 2)解决方案,项目及类之间的关系(解决方案包含项目-项目包含-类) 例如可以这样比喻: 解决方案:相当于公司 项目:相 ...

  2. 方程式0day图形化利用工具

    最近方程式的漏洞着实活了一把,分析了下githup上面的文件目录,找到了利用文件,主要是针对windows主机的SMB.RDP协议进行攻击,因为我主要根据他们提供的payload的程序,利用这两个模块 ...

  3. bzoj 3956: Count

    3956: Count Description Input Output Sample Input 3 2 0 2 1 2 1 1 1 3 Sample Output 0 3 HINT M,N< ...

  4. Codeforces Round #353 (Div. 2) E. Trains and Statistic dp 贪心

    E. Trains and Statistic 题目连接: http://www.codeforces.com/contest/675/problem/E Description Vasya comm ...

  5. LR监控linux系统资源

    一.检查系统是否安装rpc服务 使用LR监控Linux,首先查看系统是否开启了rpc服务,其次查看Linux系统守护进程rpc.restat是否启动,该进程是必须的.可以通过命令rpcinfo -p来 ...

  6. GIT(6)----fork和clone的区别,fetch与pull的区别

    参考资料: [1].Git学习笔记:fork和clone的区别,fetch与pull的区别 [2].在Github和Git上fork之简单指南

  7. SQL(insert、delete、update)执行成功,但是数据库表中无显示无记录

    如题,程序中insert一条记录,调试过程中根据执行结果发现此条sql已经执行成功(影响行数为1且插入记录已生成自增主键正确值),但是查询数据库相应表时发现表中并无相应记录,通过直接在表中插入测试数据 ...

  8. 初识云计算的三种服务模式 (IaaS SaaS PaaS)

    近期公司在使用其它云服务的同一时候.要封装自己的云服务,以下作为开发产品前的热身.来了解云计算中的三种服务模式,笔者也是从网络上查找,进行综合总结.请拍.. 三种服务模式 依据如今最经常使用.也就是比 ...

  9. 由于拷贝的文件太大,不可能一直开着SHELL,所以让SCP后台运行

    原文地址: http://blog.itpub.net/90618/viewspace-750822/ 1:开一个终端,scp命令运行后,输入密码让其拷贝 # scp chris@221.179.1. ...

  10. Android SDK目录及版本号区别

    来自:http://www.2cto.com/kf/201604/496917.html 今天又有人问Tools,Build-Tools,Platform-tools有什么区别,是干嘛的? 现在对SD ...