public static void flatten(TreeNode root) {//non-recursive
        if (root == null) return;
        if (root.left == null && root.right == null) return;

        Stack<TreeNode> stk = new Stack<TreeNode>();
        TreeNode temp = root;
        TreeNode tempchild = null;
        while (true) {
            stk.push(temp.right);
            while (temp.left != null) {
                System.out.println(temp.val);
                stk.push(temp.right);
                temp.right = temp.left;
                temp.left = null;
                temp = temp.right;
                System.out.println("curr" + temp.val);
            }
            if (stk.isEmpty() == true) break;
            tempchild = stk.pop();
            while (tempchild == null && stk.isEmpty() == false) {
                tempchild = stk.pop();
            }
            if (tempchild != null) {
                temp.right = tempchild;
                temp = temp.right;
            }
        }
    }