[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 ...
随机推荐
- Java并发(二十二):定时任务ScheduledThreadPoolExecutor
需要在理解线程池原理的基础上学习定时任务:Java并发(二十一):线程池实现原理 一.先做总结 通过一个简单示例总结: public static void main(String[] args) { ...
- 【2017多校训练08 1002】【HDOJ 6134】Battlestation Operational
典型的数列反演题. 运用莫比乌斯反演的一个结论 $[n = 1] = \sum_{d | n} \mu(d)$,将表达式做如下转化: $$ ans = \sum_{i=1}^n \sum_{j=1}^ ...
- BZOJ 4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
4554: [Tjoi2016&Heoi2016]游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4554 Descripti ...
- curl多文件上传
发送: header('Content-type:text/html; charset=utf-8'); //声明编码//模拟批量POST上传文件$url = 'http://test.cm/rece ...
- Git_简介
Git是什么? Git是目前世界上最先进的分布式版本控制系统(没有之一). Git有什么特点?简单来说就是:高端大气上档次! 那什么是版本控制系统? 如果你用Microsoft Word写过长篇大论, ...
- 【spark系列3】spark开发简单指南
分布式数据集创建之textFile 文本文件的RDDs能够通过SparkContext的textFile方法创建,该方法接受文件的URI地址(或者机器上的文件本地路径,或者一个hdfs ...
- WhatsApp & Tasker for Android – Read & Write messages
WhatsApp & Tasker for Android – Read & Write messages The requirement for the automation is ...
- datagrid在MVC中的运用09-实现排序
本文体验datagrid的排序. □ 思路 当点击datagrid的标题,视图传递给Controller的Form Data类似这样:page=1&rows=10&sort=Custo ...
- 解决Fragment每次进入都加载的问题
1.首先了解一下fragment的生命周期 onCreate是指创建该fragment类似于Activity.onCreate,你可以在其中初始化除了view之外的东西,onCreateView是创建 ...
- arcgis新版本增加的功能
1.导出拓扑错误 2.应用图层的符号设置 3.创建 SQLite 数据库 4.最小包围体 5.图形缓冲 6.按属性分割