[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 ...
随机推荐
- bzoj1402 Ticket to Ride 斯坦纳树 + 状压dp
给定\(n\)个点,\(m\)条边的带权无向图 选出一些边,使得\(4\)对点之间可达,询问权值最小为多少 \(n \leqslant 30, m \leqslant 1000\) 首先看数据范围,\ ...
- 6.13校内互测 (DP 带权二分 斜率优化)
丘中有麻plant 改自这儿,by ZBQ. 还有隐藏的一页不放了.. 直接走下去的话,如果开始时间确定那么到每个点的时间确定,把time减去dis就可以去掉路程的影响了. 这样对于减去d后的t,如果 ...
- PYQT窗口居中
#UI.py,通过UI设计师制作后直接转换为UI.py脚本 # -*- coding: utf-8 -*-from PyQt4 import QtCore, QtGui try: _fromUt ...
- BZOJ 4554: [Tjoi2016&Heoi2016]游戏 二分图匹配
4554: [Tjoi2016&Heoi2016]游戏 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4554 Descripti ...
- URAL 1881 Long problem statement
1881. Long problem statement Time limit: 0.5 secondMemory limit: 64 MB While Fedya was writing the s ...
- nginx 注册为linux系统服务
#! /bin/sh # chkconfig: - 85 15 # description: nginx is a World Wide Web server. It is used to serve ...
- 小程序获取当前页面路径url
getCurrentPages()[0].route
- HDU 3487 Play with Chain (splay tree)
Play with Chain Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 自用广告过滤规则,整合xwhyc大大的,非常小才79K
xwhyc大大 好久没更新了,自己弄了一点 更新: $third-party选项过滤多个站点的第三方广告:dy1000.com.yatu.tv,greasyfork.org 主流视频站点,请配合我的脚 ...
- 《JavaScript启示录》
<JavaScript启示录> 基本信息 作者: (美)Cody Lindley 译者: 徐涛 出版社:人民邮电出版社 ISBN:9787115334947 上架时间:2014-2-21 ...