1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| public static void fun(TreeNode root) { if (root != null) { System.out.print(root.val + ""); fun(root.left); fun(root.right); } }
public static void fun_2(TreeNode root) { Stack<TreeNode> stack = new Stack<TreeNode>(); TreeNode node = root; while(node != null || !stack.isEmpty()) { while(node != null) { System.out.print(node.val + ""); stack.push(node); node = node.left; } if (!stack.isEmpty()) { node = stack.pop(); node = node.right; } } }
|