标签:
一定一定要记住tree inorder traversal,很多变种题!
pocketGems的面经
1 public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { 2 if(root == null) { 3 return null; 4 } 5 Stack<TreeNode> stack = new Stack<TreeNode>(); 6 TreeNode cur = root; 7 TreeNode pre = null; 8 while(cur != null || !stack.isEmpty()) { 9 while(cur != null) { 10 stack.push(cur); 11 cur = cur.left; 12 } 13 cur = stack.pop(); 14 if(pre != null && pre == p) { 15 return cur; 16 } 17 pre = cur; 18 cur = cur.right; 19 } 20 return null; 21 }
标签:
原文地址:http://www.cnblogs.com/warmland/p/5794214.html