[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 ...
随机推荐
- javascript实现日期时间动态显示
.aspx代码例如以下: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Def ...
- ubuntun下安装sublime text
Sublime Text 3 是一款轻量级.跨平台的文本编辑器.可安装在ubuntu,Windows和MAC OS X上高级文本编辑软件,有一个专有的许可证,但该程序也可以免费使用,无需做逆向工程.如 ...
- log4j 2 入门实例(1)
本文介绍log4j的基本概念和将日志输出到控制台的例子. 参考文章: http://www.jianshu.com/p/464058bdbc76 http://www.hankcs.com/progr ...
- Android Weekly Notes Issue #261
Android Weekly Issue #261 June 11th, 2017 Android Weekly Issue #261 本期内容包括: Adaptive Icons; Kotlin实现 ...
- ubuntu16.04下安装wordpress
安装过程 http://blog.topspeedsnail.com/archives/4635 http://blog.topspeedsnail.com/archives/4646 碰到的问题 1 ...
- 2048聚合版开源代码,cocos2d-js编写,基于CocosEditor开发工具,可运行Android,ios,html5等
1. [代码][JavaScript]代码 /** * @GameName : * 2048 * * @DevelopTool: * Cocos2d-x Editor (CocosEd ...
- 实现远程连接MySQL
首先登录远程服务器,然后登录mysql:mysql -u用户 -p密码; 创建允许远程登录的用户并赋权:grant all privileges on 数据库.表名 to 用户名@'IP地址' ide ...
- codeforces 658C C. Bear and Forgotten Tree 3(tree+乱搞)
题目链接: C. Bear and Forgotten Tree 3 time limit per test 2 seconds memory limit per test 256 megabytes ...
- codeforces 610D D. Vika and Segments(离散化+线段树+扫描线算法)
题目链接: D. Vika and Segments time limit per test 2 seconds memory limit per test 256 megabytes input s ...
- Mac使用记录
---恢复内容开始--- brew list //查看brew安装东东 ls //当前目录下内容 brew --cache //查看brew下载目录 /usr/local/Cellar/ //隐藏文件 ...