Leetcode 285. Inorder Successor in BST
来源:互联网 发布:js给div加class 编辑:程序博客网 时间:2024/05/18 00:28
285. Inorder Successor in BST
Total Accepted: 15058 Total Submissions: 41359 Difficulty: MediumGiven a binary search tree and a node in it, find the in-order successor of that node in the BST.
Note: If the given node has no in-order successor in the tree, return null
.
Hide Similar Problems
思路:
一开始这么想的:
先判断p有没有右子树,有的话返回该子树中最小的。
否则:
用俩变量parent和grandparent记录从root track到p的last俩节点。有四种情况:
1. parent为null,也就是root == p,这时候返回null。
2. p为parent的let节点,返回parent,。
3. p为parent的右节点且parent为null或者parent是grandparent的右节点,返回null。
4. p为parent的右节点且parent是grandparent的左节点。这时候返回grandparent。
代码如下:
/** * 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(p.right != null){ p = p.right; while(p.left != null) p = p.left; return p; }else{ // p has no right children TreeNode grandparent = null; TreeNode parent = null; while(root.val != p.val){ grandparent = parent; parent = root; if(root.val > p.val) root = root.left; else root = root.right; } // case 0, p is the root if(parent == null) return null; // case 1, p is a left child if(parent.left !=null && parent.left.val == p.val) return parent; // case 2, p is the max if(grandparent == null || grandparent.right.val == parent.val) return null; else return grandparent; } }}
过不了这个case:
27 / 29 test cases passed.Input:[41,37,44,24,39,42,48,1,35,38,40,null,43,46,49,0,2,30,36,null,null,null,null,null,null,45,47,null,null,null,null,null,4,29,32,null,null,null,null,null,null,3,9,26,null,31,34,null,null,7,11,25,27,null,null,33,null,6,8,10,16,null,null,null,28,null,null,5,null,null,null,null,null,15,19,null,null,null,null,12,null,18,20,null,13,17,null,null,22,null,14,null,null,21,23]node with value 40Output:nullExpected:41
也就是应该直接返回root了。想了想,搜了搜,看到这个blog,思索了一下人家的方法为啥work就明白自己的方法为啥不work了。
我的方法是记录了最后俩父节点,但是有可能successr是grandparent的父节点,或者父节点的父节点,只要有比p.val大的,就会有满足要求的父节点。也就是上面的3错了。
所以blog中的方法简介明了,在由root -> p的过程只记录了一个变量,这个变量只在root = root.left时候被赋值,也就是说这个变量保存的是大于p的最小值。
这样上面的2-3就变成一个case了,如果p的右子树为空,返回这个刚好大于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(p.right != null){ p = p.right; while(p.left != null) p = p.left; return p; }else{ // p has no right children TreeNode res = null; while(root.val != p.val){ if(root.val > p.val){ res = root; root = root.left; }else root = root.right; } // case 0, root == null || p does not exist if(root == null) return null; // case 1, return the last node that node.val > p.val return res; } }}
0 0
- Leetcode - 285.Inorder Successor in BST
- LeetCode 285. Inorder Successor in BST
- Leetcode 285. Inorder Successor in BST
- LeetCode 285. Inorder Successor in BST
- **LeetCode-Inorder Successor in BST
- [leetcode]Inorder Successor in BST
- leetcode Inorder Successor in BST
- [LeetCode]Inorder Successor in BST
- 285. Inorder Successor in BST
- 285. Inorder Successor in BST
- [leetcode] 285. Inorder Successor in BST 解题报告
- Leetcode 285. Inorder Successor in BST (Medium) (cpp)
- [Leetcode] 285. Inorder Successor in BST 解题报告
- LeetCode 题解(266) : Inorder Successor in BST
- Inorder Successor in BST
- Inorder Successor in BST
- Inorder Successor in BST
- Inorder Successor in BST
- 剑指Offer 40题扩展 其他数字出现3次 找出只出现一次的那个 Java实现
- 剑指Offer 41题 和为s的两个数字VS 何为S的连续正数序列 Java版
- Leetcode 160. Intersection of Two Linked Lists/CC150 2.7
- 在mac上使用VS Code编写C#应用程序
- AJAX学习一
- Leetcode 285. Inorder Successor in BST
- AJAX学习二
- 数据类型与json数据格式
- BT5 upgrade to Kali
- F面经:判断数组是否单调以及Trie Tree不加wildcard 加句号
- AJAX学习三
- Android天气类app实现
- LeetCode32. Longest Valid Parentheses
- Wget 下载进度条是如何实现的?