1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | public class Solution { public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { if (root == null || p == null){ return null; } TreeNode result = null; Stack<TreeNode> stack = new Stack<TreeNode>(); while (root != null){ stack.push(root); root = root.left; } boolean findP = false; while (!stack.isEmpty()){ TreeNode node = stack.pop(); if (findP){ return node; } if (!findP && p == node){ findP = true; } if (node.right != null){ node = node.right; while (node != null){ stack.push(node); node = node.left; } } } return result; } } |
Monday, November 2, 2015
[leetcode]Inorder Successor in BST
In order 去找 加个boolean indicate 找到p,找到的话下一个就是suceessor
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment