题意

分析

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)的更多相关文章

  1. 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 ...

  2. 二叉树查找树中序后继 · Inorder Successor in Binary Search Tree

    [抄题]: 给一个二叉查找树以及一个节点,求该节点的中序遍历后继,如果没有返回null [思维问题]: 不知道分合算法和后序节点有什么关系:直接return表达式就行了,它自己会终止的. [一句话思路 ...

  3. 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 ...

  4. 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. ...

  5. 【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 ...

  6. 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 ...

  7. [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 ...

  8. 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. 解题 ...

  9. Binary Search Tree DFS Template

    Two methods: 1. Traverse 2. Divide & Conquer // Traverse: usually do not have return value publi ...

随机推荐

  1. mapreduce的一个模版

    import java.io.IOException; import java.text.DateFormat; import java.text.SimpleDateFormat; import j ...

  2. ajax实时获取下拉数据

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> ajax ...

  3. java和js互调 webview

    public class JavaAndJSActivity extends Activity implements View.OnClickListener { private EditText e ...

  4. ansible2

    一.ansible模块(yum.pip.service.conr.user.group) 你是否知道ansible一共有多少模块呢?可以用以下命令查看: [root@localhost ~]# ans ...

  5. BZOJ 3671 NOI2014 随机数生成器

    这题其实是个暴力. 首先那一堆如何构造n*m方格的东西都是在玩你. 构造出来方阵后,由于是一个排列,不存在重复,可以大力贪心. 每次将选出一个最小的元素,然后将它右上左下的元素全部打上标记(记得bre ...

  6. MVC+Ext.net零基础学习记录(四)

    在上一篇文章[MVC+Ext.net零基础学习记录(三)]中提到了利用MVC的Area可以做到项目分离,但是实际操作起来还是有很多问题的.比如,对于物理资源的访问,会报:没有相关资源 开始的时候,我在 ...

  7. vs2010 windows service 项目不能引用类库项目

    在一个windows 服务项目A中,引用了另外一个项目B,可以使用自动完成,引用其他项目中的类,按理,可以自动提示了,应该就是没问题了,但编译时却提示"未能找到类型或命名空间名称" ...

  8. sping junit test

    @ContextConfiguration(locations="classpath:spring.xml")public class BaseTest extends Abstr ...

  9. 算法(Algorithms)第4版 练习 1.3.23 1.3.22

    1.3.23 When it comes time to update t.next, x.next is no longer the original node following x, but i ...

  10. 算法(Algorithms)第4版 练习 1.3.29

    代码实现: //1.3.29 package com.qiusongde.linkedlist; import java.util.Iterator; import java.util.NoSuchE ...