• LeetCode算法题-Binary Tree Paths(Java实现-3种解法)


    这是悦乐书的第199次更新,第206篇原创

    01 看题和准备

    今天介绍的是LeetCode算法题中Easy级别的第62题(顺位题号是257)。给定二叉树,返回所有根到叶路径。例如:

    输入:

      1
     / 
    2   3
     
      5
    

    输出:[“1-> 2-> 5”,“1-> 3”]

    说明:所有根到叶路径是:1-> 2-> 5, 1-> 3

    注意:叶子是没有子节点的节点。

    本次解题使用的开发工具是eclipse,jdk使用的版本是1.8,环境是win7 64位系统,使用Java语言编写和测试。

    02 第一种解法

    特殊情况:当根节点为null的时候,直接返回空数组。

    正常情况:深度遍历,从根节点开始,依次从左节点开始,一直往左节点遍历进去,直到遇到叶子节点,此时就构成一条完整的路径,而其相邻的右叶子结点,以及从根节点开始的右节点,这些都是一条完整的路径。

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<String>();
        if (root == null) {
            return list;
        }
        helperFun(root, list, "");
        return list;
    }
    
    public void helperFun(TreeNode root, List<String> list, String path) {
        if (root.left != null) {
            helperFun(root.left, list, path + root.val + "->");
        }
        if (root.right != null) {
            helperFun(root.right, list, path + root.val + "->");
        }
        if (root.left == null && root.right == null) {
            list.add(path + root.val);
        }
    }
    

    03 第二种解法

    此解法也是利用递归,递归虽然写在方法内部了,但是思路和上面第一种解法的递归是一样的。

    public List<String> binaryTreePaths2(TreeNode root) {
        List<String> list = new ArrayList<String>();
        if (root == null) {
            return list;
        }
        if (root.left == null && root.right == null) {
            list.add(root.val+"");
            return list;
        }
        for (String path : binaryTreePaths(root.left)) {
            list.add(root.val+"->"+path);
        }
        for (String path : binaryTreePaths(root.right)) {
            list.add(root.val+"->"+path);
        }
        return list;
    }
    

    04 第三种解法

    使用迭代的方法,借助两个栈,一个存储节点信息,一个存储路径信息。

    public List<String> binaryTreePaths3(TreeNode root) {
        List<String> list = new ArrayList<String>();
        Stack<TreeNode> sNode = new Stack<TreeNode>();
        Stack<String> sStr = new Stack<String>();
        if (root == null){
            return list;
        }
        sNode.push(root);
        sStr.push("");
        while (!sNode.isEmpty()) {
            TreeNode curNode = sNode.pop();
            String curStr = sStr.pop();
            if (curNode.left == null && curNode.right == null) {
                list.add(curStr + curNode.val);
            }
            if (curNode.left != null) {
                sNode.push(curNode.left);
                sStr.push(curStr + curNode.val + "->");
            }
            if (curNode.right != null) {
                sNode.push(curNode.right);
                sStr.push(curStr + curNode.val + "->");
            }
        }
        return list;
    }
    

    05 小结

    算法专题目前已连续日更超过一个月,算法题文章62+篇,公众号对话框回复【数据结构与算法】、【算法】、【数据结构】中的任一关键词,获取系列文章合集。

    以上就是全部内容,如果大家有什么好的解法思路、建议或者其他问题,可以下方留言交流,点赞、留言、转发就是对我最大的回报和支持!

  • 相关阅读:
    4-1 软件原型设计
    3-1 案例分析
    2-1 编程作业
    1-2阅读任务
    1-1 准备工作
    第十周学习总结
    实验报告2&&第四周课程总结
    Java实验报告(一)&&第三周学习总结
    第三周编程总结
    2019年春季学期第二周作业
  • 原文地址:https://www.cnblogs.com/xiaochuan94/p/10106412.html
Copyright © 2020-2023  润新知