[Lintcode]Inorder Successor in Binary Search Tree(DFS)
题意
略
分析
1.首先要了解到BST的中序遍历是递增序列
2.我们用一个临时节点tmp储存p的中序遍历的下一个节点,如果p->right不存在,那么tmp就是从root到p的路径中大于p->val的最小数,否则就遍历p的右子树,找到最左边的节点即可
代码
class Solution {
public:
/*
* @param root: The root of the BST.
* @param p: You need find the successor node of p.
* @return: Successor of p.
*/
TreeNode * tmp;
TreeNode * inorderSuccessor(TreeNode * root, TreeNode * p) {
// write your code here'
if (root == nullptr || p == nullptr) {
return root;
}
tmp = nullptr;
if (p->right == nullptr) {
dfs(root, p);
return tmp;
}
p = p->right;
while (p->left != nullptr) {
p = p->left;
}
return p;
}
void dfs(TreeNode * root, TreeNode * p) {
if (root->val == p->val) {
return ;
}
if (root->val > p->val) {
tmp = root;
dfs(root->left, p);
}
if (root->val < p->val) {
dfs(root->right, p);
}
}
};
[Lintcode]Inorder Successor in Binary Search Tree(DFS)的更多相关文章
- 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 ...
- 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree
[抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...
- Data Structure Binary Search Tree: Inorder Successor in Binary Search Tree
struct node { int val; node *left; node *right; node *parent; node() : val(), left(NULL), right(NULL ...
- Lintcode: Remove Node in Binary Search Tree
iven a root of Binary Search Tree with unique value for each node. Remove the node with given value. ...
- 【Lintcode】095.Validate Binary Search Tree
题目: Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is define ...
- Validate Binary Search Tree(DFS)
Given a binary tree, determine if it is a valid binary search tree (BST). Assume a BST is defined as ...
- [LeetCode] Convert Sorted List to Binary Search Tree DFS,深度搜索
Given a singly linked list where elements are sorted in ascending order, convert it to a height bala ...
- LeetCode 108: Convert Sorted Array to Binary Search Tree DFS求解
Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 解题 ...
- Binary Search Tree DFS Template
Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value publi ...
随机推荐
- ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
mysql 删除表时提示有外键 mysql> drop tables auth_group;ERROR 1217 (23000): Cannot delete or update a paren ...
- (Android)react-native-splash-screen实践-解决react-native打包好后启动白屏的问题
1.安装 npm i react-native-splash-screen --save or yarn add react-native-splash-screen --save 2.自动配置 re ...
- angularJs-未加载完成的页面显示混乱ng-bind
ng-bind 未初始化完成不加载数据,避免产生{{}}导致页面混乱 还是上边的例子,当前页面没有加载完成的时候,页面显示是不完整的会频繁的出现{{}}这样子的表达式语句,给用户的感觉很不和谐,所以使 ...
- Java for LeetCode 104 Maximum Depth of Binary Tree
Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the long ...
- PAT 甲级 1065. A+B and C (64bit) (20) 【大数加法】
题目链接 https://www.patest.cn/contests/pat-a-practise/1065 思路 因为 a 和 b 都是 在 long long 范围内的 但是 a + b 可能会 ...
- 《avascript 高级程序设计(第三版)》 ---第三章 基本概念2
1.乘性操作符: 1)*法操作法: Infinity * 0 = NaN Infinity * 非零 = Infinity 或 - Infinity 2)/法操作符: Infinity / In ...
- <JAVA8新增内容>关于集合的操作(Collection/Iterator/Stream)
因为下文频繁使用lambda表达式,关于Java中的lambda表达式内容请见: http://www.cnblogs.com/guguli/p/4394676.html 一.使用增强的Iterato ...
- js修改div标签中的内容
<div id='divId'>初始文字</div> <script> $(document).ready(function(e){ $('#divId').htm ...
- (2)struts2配置祥解
struts工作流程 反射 : 1.构造对象使用构造器 //类似为Servlet public class AddAction { public AddAction(){ System.out.pri ...
- 虚拟参考站(VRS)
来源:https://www.sohu.com/a/149415053_391994 一.高精度定位 VRS是虚拟参考站(Virtual Reference Station)的简称.这项技术是CORS ...