[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 + );
} 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的更多相关文章
- 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 ...
- [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 ...
- [LintCode] Binary Tree Paths 二叉树路径
Given a binary tree, return all root-to-leaf paths.Example Given the following binary tree: 1 / \2 ...
- LintCode Binary Tree Paths
Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tre ...
- 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. ...
- Flatten Binary Tree to Linked List
Flatten a binary tree to a fake "linked list" in pre-order traversal. Here we use the righ ...
- lintcode :Invert Binary Tree 翻转二叉树
题目: 翻转二叉树 翻转一棵二叉树 样例 1 1 / \ / \ 2 3 => 3 2 / \ 4 4 挑战 递归固然可行,能否写个非递归的? 解题: 递归比较简单,非递归待补充 Java程序: ...
- [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 ...
- [LeetCode] Binary Tree Pruning 二叉树修剪
We are given the head node root of a binary tree, where additionally every node's value is either a ...
随机推荐
- BZOJ.2194.快速傅立叶之二(FFT 卷积)
题目链接 \(Descripiton\) 给定\(A[\ ],B[\ ]\),求\[C[k]=\sum_{i=k}^{n-1}A[i]*B[i-k]\ (0\leq k<n)\] \(Solut ...
- ASP.NET 构建高性能网站 第4篇
部署优化 我们都知道,不同的部署方式对站点的性能是有影响的,可能有些朋友已经知道了这点,不管怎样,我们这里还是详细系统的讲述一下这个问题,熟悉的朋友权当回顾J. Release方式编译项目 如果我们的 ...
- SQLyog客户端无法连接MySQL服务器
环境:centos下使用yum 命令安装了mysql服务 1.进入linux 通过命令service mysqld start启动mysql的服务 2.使用sqlyog 连接mysql发现连接不上,如 ...
- 极路由通过SSH添加静态路由表之后无法跳转的问题
1.确定系统已经开启了转发功能: /etc/sysctl.conf下的配置项目为net.ipv4.ip_forward = 1 2.关闭防火墙的REJECT,也就是修改/etc/config/fire ...
- ob_start()、ob_end_flush和ob_end_clean()多级缓冲
ob_start() 和 ob_end_flush() 是一对很好的搭档,可以实现对输出的控制.当成一对出现理解起来就没什么问题,但是当他们两个各自出现次数增加时,就比较难理解了. <?php ...
- 取消SVN版本号控制的bash脚本
原理非常easy,递归删除当前文件夹下全部的 .svn 文件. 把 .svn 换成 .git 就可以用于删除 git 控制
- C#程序集系列13,如何让CLR选择不同版本的程序集
本篇主要体验,在存在多个版本程序集的情况下,如何让CLR选择哪个版本程序集运行,以及程序集版本的切换. 分别生成非强名称程序集不同版本 □ 生成某个版本的程序集 →清理F盘as文件夹,剩下如下文件 → ...
- java转换emoji表情
/** * @Description 将字符串中的emoji表情转换成可以在utf-8字符集数据库中保存的格式(表情占4个字节,需要utf8mb4字符集) * @param str * 待转换字符串 ...
- iOS开源控件库收集
下拉刷新 将数据保存至keyRing 简单的模板引擎,用来生成html OmniGroup 这个其实不是类库,是一个Cocoa的Recipe CocoaPods 为XCode project提供一个类 ...
- Ext Form
表单Ext.form.FormPanel 1.表单 对于传统的b/s应用来说,数据录入元素是放在表单<form>标签里面的.而对于ExtJS应用来说,则可以直接使用FormPanel控件来 ...