public static TreeNode removeNode(TreeNode root, int value) {
        // write your code here
        TreeNode p=null, c=root;
        while (c!=null) {
            if (c.val==value)
                break;
            else if (c.val>value) {
                p=c;
                c=c.left;
            } else {
                p=c;
                c=c.right;
            }
        }
        if (c==null)
            return root;
        if (c.left==null||c.right==null) {
            if (p==null) {
                return c.left==null?c.right:c.left;
            } else {
                if (p.left==c) {
                    p.left=c.left==null?c.right:c.left;
                } else {
                    p.right=c.left==null?c.right:c.left;
                }
                return root;
            }
        } else {
            p=c;
            TreeNode t=c.right;
            while (t.left!=null) {
                p=t;
                t=t.left;
            }
            c.val=t.val;
            if (p==c) {
                p.right=null;
            } else {
                p.left=null;
            }
            return root;
        }
    }