码迷,mamicode.com
首页 > 其他好文 > 详细

285. Inorder Successor in BST

时间:2016-08-22 07:07:36      阅读:144      评论:0      收藏:0      [点我收藏+]

标签:

一定一定要记住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     }

 

285. Inorder Successor in BST

标签:

原文地址:http://www.cnblogs.com/warmland/p/5794214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!