zoukankan      html  css  js  c++  java
  • 285. Inorder Successor in BST



    /* * 285. Inorder Successor in BST * 2016-6-27 By Mingyang * 网上有些代码太复杂,我的最简单明了,无非就是一个dfs,用stack来做就好了,遇到p以后 * 用一个flag mark一下,接下来的话就可以下一个inorder的时候return就好了 * 注意:判断的是!stack.empty()而不是另外一种的方法 */ public TreeNode inorderSuccessor(TreeNode root, TreeNode p) { if(root==null) return null; Stack<TreeNode> stack=new Stack<TreeNode>(); TreeNode s=root; boolean findOrNot=false; while(!stack.empty()||s!=null){ if(s!=null){ stack.push(s); s=s.left; }else{ TreeNode temp=stack.pop(); if(findOrNot){ return temp; }else{ if(temp==p) findOrNot=true; } s=temp.right; } } return null; }
        boolean isFound=false;
        public TreeNode inorderSuccessor(TreeNode root, TreeNode p) {
            if (root == null)
                return null;
            TreeNode n = inorderSuccessor(root.left, p);
            if (n != null)
                return n;
            if (!isFound) {
                if (root.val == p.val)
                    isFound = true;
            } else {
                return root;
            }
            return inorderSuccessor(root.right, p);
        }
    
    /*
     * The idea is to compare root's value with p's value if root is not null, and consider the following two cases:
     * root.val > p.val. In this case, root can be a possible answer, so we store the root node first and call it res. 
     * However, we don't know if there is anymore node on root's left that is larger than p.val. So we move root to its left and check again.
     * root.val <= p.val. In this case, root cannot be p's inorder successor, neither can root's left child. 
     * So we only need to consider root's right child, thus we move root to its right and check again.
     * We continuously move root until exhausted. To this point, we only need to return the res in case 1.
     */
    
    
    public TreeNode inorderSuccessor1(TreeNode root, TreeNode p) {
        TreeNode res = null;
        while(root!=null) {
            if(root.val > p.val) {
                res = root;
                root = root.left;
            }
            else root = root.right;
        }
        return res;
    }
    
    
        //Successor
        public TreeNode successor(TreeNode root, TreeNode p) {
          if (root == null)
            return null;
    
          if (root.val <= p.val) {
            return successor(root.right, p);
          } else {
            TreeNode left = successor(root.left, p);
            return (left != null) ? left : root;
          }
        }
        //Predecessor
        public TreeNode predecessor(TreeNode root, TreeNode p) {
          if (root == null)
            return null;
    
          if (root.val >= p.val) {
            return predecessor(root.left, p);
          } else {
            TreeNode right = predecessor(root.right, p);
            return (right != null) ? right : root;
          }
        }
  • 相关阅读:
    Java多线程总结
    Linux命令总结
    Java笔记
    JDK7和JDK8一些重要新特性
    第八周(11.04-11.10)----每周报告
    第八周(11.04-11.10)----结对项目----逆波兰
    第八周(11.04-11.10)----个人作业----历年学生作品点评
    第七周PSP(10.27-11.03)
    第七周(10.27-11.03)----补交第六周(10.20-26)每周例行报告
    个人项目----词频统计WEB(部分功能)
  • 原文地址:https://www.cnblogs.com/zmyvszk/p/5622958.html
Copyright © 2011-2022 走看看