1415161718192021222324
public class FlattenBinaryTreeToLinkedList { public void flatten(TreeNode root) { if (root == null) return; Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode pre = null; stack.push(root); while (stack.size() > 0) { root = stack.pop(); if (root.right != null)
303132333435363738394041
solve(root.right); } public static void main(String args[]) { Tree2ListNode tl = new Tree2ListNode(); TreeNode root = new TreeNode(2); TreeNode ret = tl.flatten(root); for (int i = 0; i < 4; i ++) { System.out.println(ret.val); ret = ret.right; }