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. 2809: [Apio2012]dispatching 可并堆 左偏树

    https://www.lydsy.com/JudgeOnline/problem.php?id=2809 板子题wa了一下因为输出ans没有lld #include<iostream> ...

  2. BZOJ.2286.[SDOI2011]消耗战(虚树 树形DP)

    题目链接 BZOJ 洛谷P2495 树形DP,对于每棵子树要么逐个删除其中要删除的边,要么直接断连向父节点的边. 如果当前点需要删除,那么直接断不需要再管子树. 复杂度O(m*n). 对于两个要删除的 ...

  3. bzoj 1901: Zju2112 Dynamic Rankings -- 主席树,树状数组,哈希

    1901: Zju2112 Dynamic Rankings Time Limit: 10 Sec  Memory Limit: 128 MB Description 给定一个含有n个数的序列a[1] ...

  4. GIT(2)----入门资料,分支管理,冲突解决

    最近一直使用者GIT发现使用起来确实很不错,最近做些整理总结,发现了一些很不错的资料,收集在这里,以备忘. GIT入门挺简单的,之前有些过一篇文章,关于GIT的,但是都是一些生硬的操作,并没有系统的学 ...

  5. 使用HAproxy如何实现web站点的动静分离

    HAProxy提供高可用性.负载均衡以及基于TCP和HTTP应用的代理,支持虚拟主机,它是免费.快速并且可靠的一种解决方案.      HAProxy特别 适用于那些负载特大的web站点,这些站点通常 ...

  6. matlab进行地图仪的绘制

    % 绘制地球仪,并标出我们的位置 cla reset; load topo; [x,y,z] = sphere();%45是画出来的球面的经纬分面数 s = surface(x,y,z,'FaceCo ...

  7. 记录一次apache错误:“child pid 29023 exit signal Segmentation fault (11)”

    目前做了一台公网的测试机,主要是  php 5.3.3 版本,是 browser  --> nginx  --> apache --> php 今天因为想要安装一个商城,要求需要 P ...

  8. Extjs 事件监听

    <!DOCTYPE html> <html> <head> <title>hello-extjs</title> <meta http ...

  9. .NET:CLR via C# A Brief Look at Metadata

    基础知识 A managed PE file has four main parts: the PE32(+) header, the CLR header, the metadata, and th ...

  10. Unity中关于Device Filter的选择问题

    引言 目前工作的Unity版本是5.4.1f,发布Android版本.apk的时候,对包体的大小有些疑问,就上网查了下资料,发现Build Settings——Player Settings——Oth ...