地里新农-请到考试中心学习规则
- 积分
- 1
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2015-3-17
- 最后登录
- 1970-1-1
|
- public static String build(TreeNode root) {
- if(root == null) {
- return "";
- }
- if(root.left == null && root.right == null) {
- return root.str;
- }
- if(root.left == null || root.right == null) {
- return "";
- }
-
- StringBuilder result = new StringBuilder();
- if(root.str.equals("*") || root.str.equals("/")) {
- if(root.left.str.equals("+") || root.left.str.equals("-")) {
- result.append("(").append(build(root.left)).append(")");
- }
- else {
- result.append(build(root.left));
- }
- result.append(root.str);
- if(root.right.str.equals("+") || root.right.str.equals("-")) {
- result.append("(").append(build(root.right)).append(")");
- }
- else {
- result.append(build(root.right));
- }
- }
- else if(root.str.equals("+") || root.str.equals("-")) {
- result.append(build(root.left)).append(root.str).append(build(root.right));
- }
- return result.toString();
- }
复制代码 |
|