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. Cortex-M4 Core Registers

    Cortex-M4 Core Registers Goal: visualizing what happens to the Cortex-M4 core registers after reset ...

  2. 【推荐】腾讯android镜像(做Android开发的得好好利用下这个网站,国内的大公司还是可以滴……)

    原文地址:http://android-mirror.bugly.qq.com:8080/include/usage.html ☀ Windows I. Open Android SDK Manage ...

  3. C#编程(二十一)----------扩展方法

    C#中的扩展方法 有许多扩展类的方式.如果有类的源代码,继承就是给类添加功能的好方法.但是如果没有源代码,怎么办?吃屎可以使用扩展方法,它允许改变一个类,但不需要该类的源代码.扩展方法是静态方法,它是 ...

  4. Ubuntu14.04LTS下使用eclipse搭建Cocos2d-x的Android环境

    from://http://www.58player.com/blog-2534-94136.html 最近想玩玩游戏制作,于是选择了目前流行的游戏引擎Cocos2d-x,这个东西虽然有Android ...

  5. spring-framework-3.2.4与hibernate-release-4.3.5下使用HibernateDaoSupport抛出异常

    spring-framework-3.2.4与hibernate-release-4.3.5下使用HibernateDaoSupport抛出异常java.lang.ClassCastException ...

  6. redis + Tomcat 8 的session共享解决

    如果英文不错的看,建议直接看官网吧,官网写的挺清楚.下面的内容是转载的一篇文章,自己补充了一些,供大家参考,也欢迎大家一起讨论 官方截止到2015-10-12前是不支持Tomcat8的,详情见官网:h ...

  7. JQuery攻略(五)表单验证

    表单验证,字段空白,输入合法,数据合法....... 此章节有 1.1字段验证 1.2正则表达式验证 1.3复选框的勾选 1.1字段验证 1.字段非空 $(document).ready(functi ...

  8. 国家code和区号计算

    因为项目中要用到这个功能.实现类似微信注冊时能够选择国家并得到对应的区号.还要推断号码正确与否的正则. 找到了 libPhoneNumber-iOS 标准化电话号码库 https://github.c ...

  9. [DEFCON全球黑客大会] 针对CTF,大家都是怎么训练的?

    https://www.zhihu.com/question/30505597 https://www.zhihu.com/question/42067737

  10. 解读SSD中的Default box(Prior Box)

    1:SSD更具体的框架如下: 2: Prior Box 缩进在SSD中引入了Prior Box,实际上与anchor非常类似,就是一些目标的预选框,后续通过softmax分类+bounding box ...