SmartX interview === ![](https://i.imgur.com/2r49LEL.png) 题目1: 遍历二叉树,打印出如下字符: 25738 ```=java private void inOrder(TreeNode root) { if (root == null) { return ; } inOrder(root.left); System.out.printf("%s ", node.data); inOrder(root.right); } ``` 题目2: 二叉树中的每一个叶子节点到根节点的路径,都可以组成一个整数 如上图中有3个叶子节点,可以组成3个路径,也就是3个整数:253,753,83 遍历二叉树,打印出如下字符: 253 753 83 ``` public void traversal(TreeNode root) { Stack nodes = new Stack<>(); TreeNode current = root; while (!nodes.isEmpty() || current != null) { if (current != null) { nodes.push(current); current = current.left; } else { TreeNode node = nodes.pop(); System.out.printf("%s ", node.data); // only print leaves // nodes remain unchanged printAllStashed(nodes); current = nodes.pop(); } } } ``` 题目3: 二叉树中的每一个叶子节点到根节点的路径,都可以组成一个整数 如上图中有3个叶子节点,可以组成3个路径,也就是3个整数:253,753,83 求这 三个数整数的和,也就是 253+753+83=1089 遍历二叉树,打印出如下字符: 1089 ``` // pow(depth,10) * data public void traversalAddUp(TreeNode root) { Stack nodes = new Stack<>(); TreeNode current = root; int dep = 0; int result = 0; while (!nodes.isEmpty() || current != null) { if (current != null) { nodes.push(current); current = current.left; depth += 1; } else { TreeNode node = nodes.pop(); result += Math.pow(10, depth) * node.data; // only leaves // e.g., 200 in 253 is added, stashed 53 result += addAllStashed(nodes, depth); depth = 0; current = nodes.pop(); } } } ```