using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
  
namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Node treeRoot = CreateTree();
            scanTree(treeRoot);
        }
  
        private static void scanTree(Node treeRoot)
        {
            List<Node> list = new List<Node>();
            list.Add(treeRoot);
            Node point = treeRoot;
            Write(treeRoot);
            while (true)
            {
                if (!list.Contains(point))
                { //上一轮是移除的操作
                    if (treeRoot.leftSon == point)
                    {//移除的是左结点
                        if (treeRoot.rightSon != null)
                        {
                            treeRoot = treeRoot.rightSon;
                            list.Add(treeRoot);
                            Write(treeRoot);
                            point = treeRoot;
                            continue;
                        }
                        list.Remove(treeRoot);
                        if (list.Count == 0)
                        {
                            break;
                        }
                        point = treeRoot;
                        treeRoot = list[list.Count - 1];
                    }
                    else
                    {//移除的是右结点
                        list.Remove(treeRoot);
                        if (list.Count == 0)
                        {
                            break;
                        }
                        point = treeRoot;
                        treeRoot = list[list.Count - 1];
                    }
                    continue;
                }
  
                if (treeRoot.leftSon != null)
                {
                    treeRoot = treeRoot.leftSon;
                    Write(treeRoot);
                    list.Add(treeRoot);
                    point = treeRoot;
                    continue;
                }
                if (treeRoot.rightSon != null)
                {
                    treeRoot = treeRoot.rightSon;
                    Write(treeRoot);
                    point = treeRoot;
                    list.Add(treeRoot);
                    continue;
                }
                if (treeRoot.leftSon == null && treeRoot.rightSon == null)
                {
                    list.Remove(treeRoot);
                    if (list.Count == 0)
                    {
                        break;
                    }
                    point = treeRoot;
                    treeRoot = list[list.Count - 1];
                }
            }
  
        }
  
        public static void Write(Node node)
        {
            Console.WriteLine(node.Data);
        }
  
        private static Node CreateTree()
        {
            Node a = new Node("A");
           a.leftSon = new Node("B");
            a.rightSon = new Node("C");
            
           a.leftSon.leftSon = new Node("D");
           a.leftSon.rightSon = new Node("E");
            
           a.rightSon.leftSon = new Node("F");
           a.rightSon.rightSon = new Node("G");
  
           a.leftSon.leftSon.leftSon = new Node("H");
           a.leftSon.leftSon.rightSon = new Node("I");
            return a;
        }
    }
  
    class Node
    {
        public string Data { get; set; }
        public Node leftSon { get; set; }
        public Node rightSon { get; set; }
  
        public Node(string data)
        {
            Data = data;
        }
    }
}

C#先序遍历二叉树(非递归)

using System;
using System.Text;
using System.IO;
using System.Security.Cryptography;
  
namespace CI
{
    class CI_Encrypt
    {
        private string key;
        private string hash_type = "sha1";
  
        public CI_Encrypt(string key = "")
        {
            if(key.Length > 0)
            {
                this.key = key;
            }
        }
        public string encode(string data)
        {
            RijndaelManaged aes = new RijndaelManaged();
            aes.KeySize = 256;
            aes.BlockSize = 256;
            aes.Padding = PaddingMode.Zeros;
            aes.Mode = CipherMode.CBC;
  
            aes.Key = this.get_key(key);
            aes.GenerateIV();
            var _IV = aes.IV;
  
            byte[] buffer;
              
            ICryptoTransform AESEncrypt = aes.CreateEncryptor(aes.Key, aes.IV);
            using (MemoryStream msEncrypt = new MemoryStream())
            {
                using (CryptoStream csEncrypt = new CryptoStream(msEncrypt, AESEncrypt, CryptoStreamMode.Write))
                {
                    using (StreamWriter swEncrypt = new StreamWriter(csEncrypt))
                    {
  
                        //Write all data to the stream.
                        swEncrypt.Write(data);
                    }
                    buffer = msEncrypt.ToArray();
                }
            }
  
            var enc = new byte [ _IV.Length + buffer.Length ];
  
            Array.Copy(_IV,enc ,_IV.Length);
            Array.Copy(buffer,0,enc,_IV.Length,buffer.Length);
  
            return this.add_cipher_noise(enc);
        }
  
        public string decode(string data)
        {
            string plainText;
            byte[] _enc_data = this.remove_cipher_noise(Convert.FromBase64String(data));
            using (RijndaelManaged aes = new RijndaelManaged())
            {
                aes.KeySize = 256;
                aes.BlockSize = 256;
                aes.Padding = PaddingMode.Zeros;
                aes.Mode = CipherMode.CBC;
                aes.Key = this.get_key(key);
                var _IV = new byte[aes.IV.Length];
                Array.Copy(_enc_data, _IV, _IV.Length);
                aes.IV = _IV;
                var endata = new byte[_enc_data.Length - aes.IV.Length];
                Array.Copy(_enc_data, aes.IV.Length, endata, 0, endata.Length);
  
                // Create a decrytor to perform the stream transform.
                ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
  
                // Create the streams used for decryption. 
                using (MemoryStream msDecrypt = new MemoryStream(endata))
                {
                    using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
                    {
                        using (StreamReader srDecrypt = new StreamReader(csDecrypt))
                        {
  
                            // Read the decrypted bytes from the decrypting stream 
                            // and place them in a string.
                            plainText = srDecrypt.ReadToEnd();
                        }
                    }
                }
  
            }
            return plainText;
        }
  
        public string add_cipher_noise(byte[] data)
        {
            string hashkey = this.hash(System.Text.Encoding.Default.GetString(this.get_key(this.key)));
            char[] keyChar = hashkey.ToCharArray();
            byte[] encrpytData = new byte[data.Length];
            for (int i = 0, j = 0; i < data.Length; i++, j++)
            {
                if (j >= hashkey.Length)
                {
                    j = 0;
                }
                var temp = (((int)data[i] + (int)keyChar[j]) % 256);
                encrpytData[i] = (byte)temp;
            }
            return System.Convert.ToBase64String(encrpytData);
        }
  
        public byte[] remove_cipher_noise(byte[] data)
        {
            string hashkey = this.hash(System.Text.Encoding.Default.GetString(this.get_key(this.key)));
            char[] keyChar = hashkey.ToCharArray();
            byte[] encrpytData = new byte[data.Length];
            int temp;
  
            for (int i = 0, j = 0; i < data.Length; i++, j++)
            {
                if (j >= hashkey.Length)
                {
                    j = 0;
                }
                temp = (int)data[i] - (int)keyChar[j];
                if(temp < 0)
                {
                    temp += 256;
                }
                encrpytData[i] = (byte)temp;
            }
            return encrpytData;
        }
  
        private string hash(string key)
        {
            byte[] hashBytes;
            if (this.hash_type == "sha1")
            {
                SHA1 sha = new SHA1CryptoServiceProvider();
                hashBytes = (sha.ComputeHash(System.Text.Encoding.Default.GetBytes(key)));
            }
            else
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                hashBytes = (md5.ComputeHash(System.Text.Encoding.Default.GetBytes(key)));
            }
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return sb.ToString().ToLower();
        }
  
        private byte[] get_key(string key)
        {
            MD5 md5 = new MD5CryptoServiceProvider();
            byte[] hashBytes = (md5.ComputeHash(System.Text.Encoding.UTF8.GetBytes(key)));
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < hashBytes.Length; i++)
            {
                sb.Append(hashBytes[i].ToString("X2"));
            }
            return System.Text.Encoding.Default.GetBytes(sb.ToString().ToLower());
        }
    }
}

用C#实现跟CI里面的加密解密

package chu;
  
import java.util.ArrayList;
  
/**
 * @author chufazheng@gmail.com
 * update: 2014-11-26
 */
public final class SudokuSolver {
  
    private ArrayList<Integer> blank;
    private ArrayList<Integer> fixed;
    private ArrayList<TrialPoint> trials;
    private int[] cell = new int[81];
  
    public static void main(String[] arg) {
        String str;
        if (arg != null && arg.length > 0 && arg[0].length() >= 81) {
            str = arg[0];
        } else {
            //str="000000500000008300600100000080093000000000020700000000058000000000200017090000060";//the least given
            str = "800000000003600000070090200050007000000045700000100030001000068008500010090000400";//the world hardest
        }
        SudokuSolver game = new SudokuSolver();
        game.load(str);
        game.solve();
    }
  
    public void solve() {
        if (fixed.size() < 17) {
            System.out.println("Invalid game!");
            System.exit(0);
        }
        showBoard(true);
        long start = System.currentTimeMillis();
        if (!basicSolve()) {
            trialSolve();
        }
        long end = System.currentTimeMillis();
        showBoard(false);
        if (check()) {
            System.out.println("[" + (end - start) + "ms OK!]");
        } else {
            System.out.println("[" + (end - start) + "ms, cannot solve it, are you sure it has solution?");
        }
    }
  
    public SudokuSolver() {
        blank = new ArrayList();
        fixed = new ArrayList();
        trials = new ArrayList();
    }
  
    public void load(String str) {
        blank.clear();
        fixed.clear();
        char[] chr = str.toCharArray();
        int i = 0, j = 0, len = chr.length;
        while (i < len && j < 81) {
            if (Character.isDigit(chr[i])) {
                if (chr[i] == '0') {
                    cell[j] = 0x1FF;
                    blank.add(j);
                } else {
                    cell[j] = 1 << chr[i] - 49;
                    fixed.add(j);
                }
                j++;
            }
            i++;
        }
        //if the numbers input less than 81, add '0's.
        for (;j < 81; j++) {
            cell[j] = 0x1FF;
            blank.add(j);
        }
    }
  
    private void reset(int[] data) {
        blank.clear();
        fixed.clear();
        System.arraycopy(data, 0, cell, 0, 81);
        for (int i = 0; i < 81; i++) {
            if (Integer.bitCount(cell[i]) != 1) {
                blank.add(i);
            } else {
                fixed.add(i);
            }
        }
    }
  
    private void showCell(int idx, boolean hide) {
        if (hide) {
            if (Integer.bitCount(cell[idx]) == 1) {
                System.out.print("[" + (Integer.numberOfTrailingZeros(cell[idx]) + 1) + "]");
            } else {
                System.out.print("[ ]");
            }
        } else {
            StringBuilder sb = new StringBuilder();
            sb.append('[');
            for (int i = 0; i < 9; i++) {
                if ((cell[idx] >>> i & 1) == 1) {
                    sb.append(i + 1);
                }
            }
            sb.append(']');
            System.out.print(sb);
        }
    }
  
    private void showBoard(boolean hide) {
        System.out.print("---------------------------");
        for (int i = 0; i < 81; i++) {
            if (i % 9 == 0) {
                System.out.print("\\n");
            }
            showCell(i, hide);
        }
        System.out.println("\\n---------------------------");
    }
  
    private boolean updateCandidates(ArrayList<Integer> fixed) {
        for (int i : fixed) {
            int opt = 0x1FF ^ cell[i];
            for (int j : X(i)) {
                cell[j] &= opt;
                //!notice
                if (cell[j] == 0) {
                    return false;
                }
            }
        }
        return true;
    }
  
    private void seekFixedable() {
        fixed.clear();
        for (int i : blank) {
            if (Integer.bitCount(cell[i]) == 1) {
                fixed.add(i);
                //System.out.println("fixed:"+i+"=>"+cell[i]);
            }
        }
        blank.removeAll(fixed);
    }
  
    private boolean check() {
        int[] checkpoint = new int[]{0, 12, 24, 28, 40, 52, 56, 68, 80};
        for (int i : checkpoint) {
            int r, b, c;
            r = b = c = cell[i];
            for (int j = 0; j < 8; j++) {
                r ^= cell[X(i)[j]];
                c ^= cell[X(i)[8 + j]];
                b ^= cell[X(i)[16 + j]];
            }
            if ((r & b & c) != 0x1FF) {
                return false;
            }
        }
        return true;
    }
  
    private void seekUniqueCandidate() {
        for (int cel_idx : blank) {
            int row = 0, col = 0, box = 0;
            for (int i = 0; i < 8; i++) {
                row |= cell[X(cel_idx)[i]];
                col |= cell[X(cel_idx)[8 + i]];
                box |= cell[X(cel_idx)[16 + i]];
            }
            if (Integer.bitCount(cell[cel_idx] & ~row) == 1) {
                cell[cel_idx] &= ~row;
                continue;
            }
            if (Integer.bitCount(cell[cel_idx] & ~col) == 1) {
                cell[cel_idx] &= ~col;
                continue;
            }
            if (Integer.bitCount(cell[cel_idx] & ~box) == 1) {
                cell[cel_idx] &= ~box;
            }
        }
    }
  
    private void seekMutexCell() {
        ArrayList<Integer> two = new ArrayList();
        for (int i : blank) {
            if (Integer.bitCount(cell[i]) == 2) {
                two.add(i);
            }
        }
        for (int i = 0; i < two.size(); i++) {
            for (int j = i + 1; j < two.size(); j++) {
                if (cell[two.get(i)] == cell[two.get(j)]) {
                    int opt = ~cell[two.get(i)];
                    if (two.get(i) / 9 == two.get(j) / 9) {//same row
                        for (int n = 0; n < 8; n++) {
                            cell[X(two.get(i))[n]] &= opt;
                        }
                    }
                    if ((two.get(i) - two.get(j)) % 9 == 0) {//same col                        
                        for (int n = 8; n < 16; n++) {
                            cell[X(two.get(i))[n]] &= opt;
                        }
                    }
                    if ((two.get(i) / 27 * 3 + two.get(i) % 9 / 3) == (two.get(j) / 27 * 3 + two.get(j) % 9 / 3)) {//same box
                        for (int n = 16; n <24; n++) {
                            cell[X(two.get(i))[n]] &= opt;
                        }
                    }
                    cell[two.get(j)] = ~opt;
                }
            }
        }
    }
  
    private boolean basicSolve() {
        do {
            if (!updateCandidates(fixed)) {
                backForward();
            }
            seekUniqueCandidate();
            seekMutexCell();
            seekFixedable();
        } while (!fixed.isEmpty());
        return blank.isEmpty();
    }
  
    private boolean setTrialCell() {
        for (int i : blank) {
            if (Integer.bitCount(cell[i]) == 2) {
                int trialValue = 1 << Integer.numberOfTrailingZeros(cell[i]);
                int waitingValue = cell[i] ^ trialValue;
                //System.out.println("Try:[" + i + "]->" + (Integer.numberOfTrailingZeros(trialValue) + 1) + "#" + (Integer.numberOfTrailingZeros(waitingValue) + 1));
                cell[i] = trialValue;
                trials.add(new TrialPoint(i, waitingValue, cell));
                return true;
            }
        }
        return false;
    }
  
    private void backForward() {
        if (trials.isEmpty()) {
            System.out.println("Can not go backforword, no trial point, maybe no solution!");
            return;
        }
        TrialPoint back = trials.get(trials.size() - 1);
        trials.remove(back);
        reset(back.cel);
        cell[back.idx] = back.val;
        fixed.add(back.idx);
        //System.out.println("Back:[" + back.idx + "]->" + (Integer.numberOfTrailingZeros(back.val) + 1));
    }
  
    private void trialSolve() {
        while (!blank.isEmpty()) {
            if (setTrialCell()) {
                basicSolve();
            } else {
                if (trials.isEmpty()) {
                    //System.out.println("Maybe no solution.");
                    break;
                } else {
                    backForward();
                    basicSolve();
                }
            }
        }
    }
    private int[] X(int idx){
        int neighbors[]=new int[24];
        int box[]=new int[]{0,1,2,9,10,11,18,19,20};
        int r=idx/9,c=idx%9,xs=idx/27*27+idx%9/3*3;
        int i=0;
        for(int n=0;n<9;n++){
            if(n==c)continue;
            neighbors[i++]=r*9+n;
        }
        for(int n=0;n<9;n++){
            if(n==r)continue;
            neighbors[i++]=c+n*9;
        }
        for(int n=0;n<9;n++){
            int t=xs+box[n];
            if(t==idx)continue;
            neighbors[i++]=t;
        }
          return neighbors;
    }    
}
final class TrialPoint {
  
    public int idx;
    public int val;
    public int[] cel = new int[81];
  
    public TrialPoint(int i, int v, int[] board) {
        idx = i;
        val = v;
        System.arraycopy(board, 0, cel, 0, 81);
    }
}

数独解法Java实现

// 倒置字符串 修改版,再次修改版
static public string Reverse( string s )
{
    char[] charArray = s.ToCharArray();
    Array.Reverse( charArray );
    return new string( charArray );
}

C#倒置字符串方法