leetcode 在本地IDE debug调试TreeNode树相关算法代码

前言

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函数即可,可以在想打断点的地方尽情打了。

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Fourier_1024

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值