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. Using an LPC-Link2 as an LPC4370 evaluation board

    https://www.lpcware.com/content/faq/lpcxpresso/using-lpclink2-as-lpc4370-eval As well as being a sta ...

  2. js文件改变之后浏览器缓存问题怎么解决?

    升级了js文件,很多页面都引用了这个文件,需要主动清除浏览器缓存才会生效,有没有什么办法可以不主动清除就可以? 修改文件名,加上版本号,或 xxx.js?v=0.101

  3. 关于云计算基础架构IaaS层的几点看法

    真实的云计算什么样? 云计算对普通用户来说,总是一个云里雾里的话题. 本文从最基础的概念開始科普,说明了四个常见的错误理解,和作者的四个猜想. IaaS(Infrastructure as a Ser ...

  4. AngularJS双向绑定,手动实施观察

    实现这样的一个需求:页面中某个地方显示某个文本框的值经过计算得到的结果,而且是文本框值每次变化显示的计算结果也跟着动态变化. 在controller中可以声明一个对象,它的一个字段用来存储初始值: $ ...

  5. 集群服务器下使用SpringBoot @Scheduled注解定时任务

    原文:https://blog.csdn.net/huyang1990/article/details/78551578 SpringBoot提供了 Schedule模块完美支持定时任务的执行 在实际 ...

  6. Odoo(OpenERP)应用实践:代发货管理

    原文地址:http://blog.csdn.net/wangnan537/article/details/47091857 有些分销商,在买家下单后才向供应商采购产品,并由供应商直接发货给客户.这种模 ...

  7. Swift:playground

    在介绍Playground之前,我先罗列一些本人认为有点重要然而零碎的知识点. 1. Swift语法.每句话之后不用加分号.但也能够加分号.但假设写在一行的话.必须加分号. 2. Swift严格要求变 ...

  8. 在 Spring 4.3.9下升级 Velocity 1.7.x to Velocity 2.0.x 出现的问题

    1: Spring 的  spring-context-support 报错 java.lang.NoClassDefFoundError: org/apache/velocity/runtime/l ...

  9. spring 深入reading

    http://wenku.baidu.com/view/8db141624a7302768e9939b3.html http://docs.spring.io/spring/docs/4.2.1.BU ...

  10. 启明星Helpdesk与微信配置说明

    启明星Helpdesk支持微信版,本文将介绍启明星Helpdesk与微信配置的说明.