For the given tree, in order traverse is:

  • visit left side
  • root
  • visit right side
// 6,8,10,11,12,15,16,17,20,25,27

The successor is the one right next to the target:

// target 8 --> succossor is 10

So, given the tree and target node, to find its successor.

Require knowledge how recurise call work, mainly it should reach the leaf node, then print it from bottom to top. For the node which has both right and left side, it visits left first, then itself, then right side.

function findCurrent(root, val) {
let current = root;
let found = null;
while (current !== null) {
if (current.val === val) {
found = current;
break;
} else if (current.val < val) {
current = current.right;
} else {
current = current.left;
}
} return found;
} function findMin(root) {
// min should be on the left side
while (root.left != null) {
root = root.left;
} return root;
} function findSuccsor(root, val) {
const current = findCurrent(root, val);
if (current == null) {
return null;
}
// case 1: Node has right subtree
// Find min on the right part of the node
// the min should be on the left most
if (current.right != null) {
return findMin(current.right);
}
// case 2: Node has no right subtree
// --> A. target node can be on the left side like 8
// --> B. target node can be on the right side like 12
else {
let parent = root;
let found = null;
while (parent !== current) {
// A: if target is smaller than parent, means on the left side
// then we keep going down to find target, its parent should be
// the successor
if (current.val < parent.val) {
found = parent;
parent = parent.left;
}
// B: it target is larger than parent, means on the right side
// then it means the parent node should already been visited
// so we should not set successor in this case
else {
parent = parent.right;
}
} return found;
}
} function Node(val) {
return {
val,
left: null,
right: null
};
} function Tree() {
return {
root: null,
addLeft(val, root) {
const node = Node(val);
root.left = node;
return root.left;
},
addRight(val, root) {
const node = Node(val);
root.right = node;
return root.right;
}
};
} const tree = new Tree();
const root = Node();
tree.root = root;
const n1 = tree.addLeft(, tree.root);
const n2 = tree.addRight(, tree.root); const n3 = tree.addLeft(, n1);
tree.addLeft(, n3); const n4 = tree.addRight(, n1);
tree.addLeft(, n4); const n5 = tree.addLeft(, n2);
const n6 = tree.addLeft(, n5);
const n7 = tree.addRight(, n2);
tree.addRight(, n7);
// inorder
// 6,8,10,11,12,15,16,17,20,25,27
// successor is the one next to the target
// target 8 --> succossor is 10
console.log(findSuccsor(tree.root, ));

[Algorithm] Inorder Successor in a binary search tree的更多相关文章

  1. Verify Preorder/Inorder/Postorder Sequence in Binary Search Tree

    Verify Preorder Sequence in Binary Search Tree \Given an array of numbers, verify whether it is the ...

  2. [Algorithm] Delete a node from Binary Search Tree

    The solution for the problem can be divided into three cases: case 1: if the delete node is leaf nod ...

  3. 【LeetCode OJ】Recover Binary Search Tree

    Problem Link: https://oj.leetcode.com/problems/recover-binary-search-tree/ We know that the inorder ...

  4. Inorder Successor in Binary Search Tree

    Given a binary search tree (See Definition) and a node in it, find the in-order successor of that no ...

  5. [线索二叉树] [LeetCode] 不需要栈或者别的辅助空间,完成二叉树的中序遍历。题:Recover Binary Search Tree,Binary Tree Inorder Traversal

    既上篇关于二叉搜索树的文章后,这篇文章介绍一种针对二叉树的新的中序遍历方式,它的特点是不需要递归或者使用栈,而是纯粹使用循环的方式,完成中序遍历. 线索二叉树介绍 首先我们引入“线索二叉树”的概念: ...

  6. [Leetcode][JAVA] Recover Binary Search Tree (Morris Inorder Traversal)

    Two elements of a binary search tree (BST) are swapped by mistake. Recover the tree without changing ...

  7. Binary Search Tree In-Order Traversal Iterative Solution

    Given a binary search tree, print the elements in-order iteratively without using recursion. Note:Be ...

  8. leetcode@ [173] Binary Search Tree Iterator (InOrder traversal)

    https://leetcode.com/problems/binary-search-tree-iterator/ Implement an iterator over a binary searc ...

  9. 二叉树前序、中序、后序非递归遍历 144. Binary Tree Preorder Traversal 、 94. Binary Tree Inorder Traversal 、145. Binary Tree Postorder Traversal 、173. Binary Search Tree Iterator

    144. Binary Tree Preorder Traversal 前序的非递归遍历:用堆来实现 如果把这个代码改成先向堆存储左节点再存储右节点,就变成了每一行从右向左打印 如果用队列替代堆,并且 ...

随机推荐

  1. Sublime Text 2搭建Go开发环境,代码提示+补全+调试

    本文在已安装Go环境的前提下继续. 1.安装Sublime Text 2 2.安装Package Control. 运行Sublime,按下 Ctrl+`(`在Tab键上边),然后输入以下内容: im ...

  2. .NET开源作业调度框架(Quartz.NET和FluentScheduler)实战项目演练

    一.课程介绍 明人不说暗话,跟着阿笨一起玩NET .本次分享课程属于<C#高级编程实战技能开发宝典课程系列>中的一部分,阿笨后续会计划将实际项目中的一些比较实用的关于C#高级编程的技巧分享 ...

  3. 关于流媒体(m3u8)的下载与播放

    求助:关于流媒体(m3u8)的下载与播放 http://www.cocoachina.com/bbs/read.php?tid-93389.html 此文有相关讨论.demo等,可关注.

  4. 缩放到被选择的部分: ICommand Cmd = new ControlsZoomToSelectedCommandClass();

    AddItem("esriControls.ControlsZoomToSelectedCommand"); //ICommand Cmd = new ControlsZoomTo ...

  5. jQuery Ajax 上传文件改进

    如果用户取消上传后 背景 提示自动消失了.... 修正Bug.... 同时也更新了不同上传类型的提示字体大小... 2017-05-26 增加了鼠标释放提示 先看之前的效果: 再看现在的效果: 升级 ...

  6. 单身毒妈第一至八季/全集Weeds迅雷下载

    本季第一.二.三.四季 Weeds (2005-2008) 看点:由于丈夫的意外身亡,Nancy Botwin成了一名膝下抚有两子的单身母亲,一无所长的她找不到一份能养活一家三口的工作,但天无绝人之路 ...

  7. Material Designer的低版本兼容实现(一)—— 简介 & 目录

    很长一段时间没写东西了,其实是因为最近在研究Material Designer这个东西,熬夜熬的身体也不是很好了.所以就偷懒没写东西,这回开的这个系列文章是讲如何将Material Designer在 ...

  8. [Web 前端] 如何构建React+Mobx+Superagent的完整框架

    ReactJS并不像angular一样是一个完整的前端框架,严格的说它只是一个UI框架,负责UI页面的展示,如果用通用的框架MVC来说,ReactJs只负责View了,而Angular则是一个完整的前 ...

  9. RF的特征子集选取策略(spark ml)

    支持连续变量和类别变量,类别变量就是某个属性有三个值,a,b,c,需要用Feature Transformers中的vectorindexer处理 上来是一堆参数 setMaxDepth:最大树深度 ...

  10. python的重试库tenacity用法以及类似库retry、requests实现

    介绍 tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simpli ...