653. Two Sum IV – Input is a BST

653. Two Sum IV – Input is a BST

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
class Solution {
  public boolean findTarget(TreeNode root, int k) {
		ArrayList data = new ArrayList();
		traverers(root, data);
		for (int i  = 0 ; i < data.size() ; i++)
			for (int j  = 0 ; j < data.size() && i!=j ; j++)
				if (data.get(i) + data.get(j) == k)
					return true;

		return false;
	}

	private void traverers(TreeNode root, ArrayList data) {
		if (root != null) {
			data.add(root.val);
			traverers(root.left, data);
			traverers(root.right, data);
		}
	}
}

590. N-ary Tree Postorder Traversal

590. N-ary Tree Postorder Traversal

/*
// Definition for a Node.
class Node {
    public int val;
    public List children;

    public Node() {}

    public Node(int _val,List _children) {
        val = _val;
        children = _children;
    }
};
*/
class Solution {
   	public List postorder(Node root) {
		List out = new ArrayList();
		postorder(root, out);
		return out;
	}

	private void postorder(Node root, List out) {
		if( null != root){
		for (int i = 0; i < root.children.size(); i++)
			postorder(root.children.get(i), out);
		out.add(root.val);
        }
	}
}

788. Rotated Digits

788. Rotated Digits

class Solution {
 	public static int rotatedDigits(int N) {

		int good = N;
		for (int i = 1; i <= N; i++) {
			boolean valid = true;
			int current = i;
			int converted = i;
			inner: {
				int m = 1;
				while (current != 0) {
					int digit = current % 10; // get current digit

					// Convert digits regard to the specific rule
					if (digit == 2) {
						converted -= (digit * m);
						converted += (5 * m);
					} else if (digit == 5) {
						converted -= (digit * m);
						converted += (2 * m);
					} else if (digit == 6) {
						converted -= (digit * m);
						converted += (9 * m);
					} else if (digit == 9) {
						converted -= (digit * m);
						converted += (6 * m);
					}

					if (digit == 3 || digit == 4 || digit == 7) {
						valid = false;
						good--;
						break inner;
					}
					current /= 10;
					m *= 10;// next digit
				}

			}
			if (converted == i && valid)
				good--;
		}
		return good;
	}
}