Inorder Successor in Binary Search Tree
来源:互联网 发布:api接口文档 源码 编辑:程序博客网 时间:2024/05/21 19:40
Given a binary search tree (See Definition) and a node in it, find the in-order successor of that node in the BST.
If the given node has no in-order successor in the tree, return null
.
Notice
It's guaranteed p is one node in the given tree. (You can directly compare the memory address to find p)
/** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */public class Solution { public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { if(root == null) { return root; } TreeNode successor = null; while(root != null && root.val != p.val) { if(root.val > p.val) { successor = root; root = root.left; } else { root = root.right; } }//p is found//p doesn't have right subtree if(root.right == null) { return successor; }//get the most left child of the right subtree root = root.right; while(root.left != null) { root = root.left; } return root; }}
Notice that this is BST. So the successor node will be at two possible position:
its last parent node that is larger then itself;
the leftmost child of its right subtree
0 0
- Inorder Successor in Binary Search Tree
- Inorder Successor in Binary Search Tree
- [Google] Inorder Successor in Binary Search Tree, Solution
- Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点
- Tree_Graph Inorder Successor in Binary Search Tree BST中找中序遍历的后继节点 @CareerCup
- Facebook 面试题 Binary tree inorder successor
- Inorder Successor in BST
- Inorder Successor in BST
- Inorder Successor in BST
- Inorder Successor in BST
- Inorder Successor in BST
- [刷题]Inorder Successor in BST
- **LeetCode-Inorder Successor in BST
- [LeetCode285]Inorder Successor in BST
- [leetcode]Inorder Successor in BST
- leetcode Inorder Successor in BST
- [LeetCode]Inorder Successor in BST
- 285. Inorder Successor in BST
- java内存分配和String类型的深度解析
- 用wow.js实现滚动页面时触发CSS 动画效果的插件
- 堆、栈、程序区、静态数据区、字符常量区的介绍与区别
- MAVEN pom.xml文件添加依赖报错
- CI框架在Apache服务器下去掉URL中的index.php
- Inorder Successor in Binary Search Tree
- 使用Areas分离ASP.NET MVC项目
- 微信小程序,引爆新热点!JEECG社区小程序实战培训,业内首发,实战干货!
- EDGE浏览器配合阿呆喵设置广告过滤
- 解决weblogic与系统时间相差8小时的问题
- 得到图片字节流 用bitmap显示出来
- eclipse中egit插件冲突处理
- ASP.NET 之CSS学习
- git使用情景1:正在写代码,突然线上出现了bug