java - 二叉树和 NullPointerException

标签 java recursion binary-tree

此方法采用整数树并构造以下字符串:

(根部的数据,左子树的String,右子树的String)

例如,如果变量树存储对以下树的引用:

          +---+
          | 2 |
          +---+
         /     \
     +---+     +---+
     | 8 |     | 1 |
     +---+     +---+
    /         /     \
+---+     +---+     +---+
| 0 |     | 7 |     | 6 |
+---+     +---+     +---+
         /               \
     +---+               +---+
     | 4 |               | 9 |
     +---+               +---+

它应该返回一个字符串:

"(2, (8, 0, empty), (1, (7, 4, empty), (6, empty, 9)))"

对于空树,该方法应返回“empty”。对于叶节点,它应该以字符串形式返回节点中的数据。对于分支节点,它应该返回一个带括号的字符串,该字符串包含三个以逗号分隔的元素:

使用我的方法,它有时会起作用,但有时会生成 NullPointerException。谁能指出发生这种情况的地点和原因?

这是我的:

 private IntTreeNode overallRoot; // first node; linked to other nodes

 // post: returns the string of all data in leaves. For a branch node, return a parenthesized String
 //       w/ three elementsseparated by commas. return empty for empty tree
 public String toString2() {
     if (overallRoot == null) { // If empty tree
         return "empty";
     } else {
         return toString2(overallRoot);
     }
 }

 // helper for toString
 private String toString2(IntTreeNode root) {
     String value = "";
     if (root.left != null && root.right != null) { // If both branches exist
         value += "(" + root.data + ", " + toString2(root.left)+ ", " + toString2(root.right) + ")";  
     } else if (root.left != null && root.right == null) { // if right branch is empty
         value += "(" + root.data + ", " + toString2(root.left) + ", empty)";
     } else if (root.left == null && root.right != null) { // if left branch is empty
         value += "(" + root.data + ", empty, " + toString2(root.left) + ")";
     } else { // If at a leaf
         return "" + root.data;
     }
     return value;
 }

这是节点类:

public class IntTreeNode {
     public int data;
     public IntTreeNode left;
     public IntTreeNode right;

     // constructs a leaf node with given data
     public IntTreeNode(int data) {
         this(data, null, null);
     }

     // constructs a branch node with given data, left subtree,
     // right subtree
     public IntTreeNode(int data, IntTreeNode left, IntTreeNode right) {
         this.data = data;
         this.left = left;
         this.right = right;
     }
}

最佳答案

错误在这里:

else if (root.left == null && root.right != null) { // if left branch is empty
         value += "(" + root.data + ", empty, " + toString2(root.left) + ")";
}

应该是……

else if (root.left == null && root.right != null) { // if left branch is empty
         value += "(" + root.data + ", empty, " + toString2(root.right) + ")";
}

...,将 root.right 而不是 root.left 传递给 toString2

关于java - 二叉树和 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25151268/

相关文章:

haskell - 一个 Haskell 问题写错误发现洞 _ a5

java - 我可以将 Tomcat Valve 与 Jetty 一起使用吗?

java - 结果的 JSON 格式不正确

java - 如何在 Java 中递归地从 N 元素集生成所有 k 元素子集

algorithm - 将后序二叉树遍历索引转换为层序(广度优先)索引

java - java中的二叉搜索树,主要不是从BST读取

java-native-interface - jdk1.0 为何缺乏原生代码和 Java 代码之间的清晰分离?

java - ClassCastException 在 Arrays.sort(...) 中使用转换后的列表

C++ 模板化映射包含自身

任何前两个数字的java斐波那契