前言
LeetCode中有很多关于二叉树的代码,给的树的形式是形如[10,5,-3,3,2,null,11,3,-2,null,1]的字符串格式,是一个通过层序遍历形成的字符串。
很多时候希望能够在本地调试代码,需要
- 导出TreeNode结构
- 解析字符串为TreeNode对象
代码结构
调试步骤
导出TreeNode
TreeNode.java
public class TreeNode {
int val;
TreeNode left;
TreeNode right;
TreeNode() {
}
TreeNode(int val) {
this.val = val;
}
TreeNode(int val, TreeNode left, TreeNode right) {
this.val = val;
this.left = left;
this.right = right;
}
}
解析字符串
StrToTreeNode.java
参考代码
import java.util.LinkedList;
import java.util.Queue;
/**
* 使用方法:
* StrToTreeNode strToTreeNode = new StrToTreeNode();
* TreeNode root = strToTreeNode.createTree("10,5,-3,3,2,null,11,3,-2,null,1");
* strToTreeNode.printTree(root);
*/
public class StrToTreeNode {
public TreeNode createTree(String tree) {
// {1,2,3,4,#,#,#,5,#,6,#,7,#,8}
String[] ss = tree.split(",");
return createTree(ss);
}
public TreeNode createTree(String[] tree) {
Queue<TreeNode> q = new LinkedList<TreeNode>();
// 1st one should not be #
TreeNode root = constructOne(tree[0]);
q.add(root);
int idx = 1;
while (!q.isEmpty()) {
TreeNode tn = q.poll();
if (tn == null) {
continue;
}
// construct tn's left&right node
// when to stop
if (idx == tree.length) {
break;
}
TreeNode left_ = constructOne(tree[idx]);
tn.left = left_;
q.add(left_);
idx++;
if (idx == tree.length) {
break;
}
TreeNode right_ = constructOne(tree[idx]);
idx++;
tn.right = right_;
// add to queue
q.add(right_);
}
return root;
}
private void printNode(TreeNode tn, int indent) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < indent; i++) {
sb.append("\t");
}
sb.append(tn.val);
System.out.println(sb.toString());
}
public void printTree(TreeNode root, int indent) {
if (root == null) {
return;
}
// if (root.left == null && root.right == null) {
// printNode(root, indent);
// }
// right
printTree(root.right, indent + 1);
// self
printNode(root, indent);
// left
printTree(root.left, indent + 1);
}
public void printTree(TreeNode root) {
// right first
printTree(root, 0);
}
private TreeNode constructOne(String s) {
if (s.compareTo("null") == 0) {
return null;
} else {
return new TreeNode(Integer.parseInt(s));
}
}
}
粘贴Sollution
Solution.java
例如力扣437题,官方solution
class Solution {
public int pathSum(TreeNode root, int targetSum) {
if (root == null) {
return 0;
}
int ret = rootSum(root, targetSum);
ret += pathSum(root.left, targetSum);
ret += pathSum(root.right, targetSum);
return ret;
}
public int rootSum(TreeNode root, int targetSum) {
int ret = 0;
if (root == null) {
return 0;
}
int val = root.val;
if (val == targetSum) {
ret++;
}
ret += rootSum(root.left, targetSum - val);
ret += rootSum(root.right, targetSum - val);
return ret;
}
}
编写测试代码
Test.java
public class Test {
public static void main(String[] args) {
StrToTreeNode strToTreeNode = new StrToTreeNode();
TreeNode root = strToTreeNode.createTree("10,5,-3,100,2,null,11,3,-2,null,1");
// strToTreeNode.printTree(root);
Solution solution = new Solution();
int sum = solution.pathSum(root, 8);
System.out.println(sum);
}
}
运行测试代码
直接运行Test中的main函数即可,可以在想打断点的地方尽情打了。