257.二叉树的所有路径

递归法

java 复制代码
public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();// 存最终的结果
        if (root == null) {
            return res;
        }
        List<Integer> paths = new ArrayList<>();// 作为结果中的路径
        traversal(root, paths, res);
        return res;
    }

    public void traversal(TreeNode t, List<Integer> paths, List<String> res) {
        // 前序遍历 中
        paths.add(t.val);

        if (t.left == null && t.right == null) {
            // t是叶子节点,该收集结果了
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < paths.size() - 1; i++) {
                sb.append(paths.get(i));
                sb.append("->");
            }
            sb.append(paths.get(paths.size() - 1));
            res.add(sb.toString());
            return;
        }
        if (t.left != null) {
            // 左节点不是空
            traversal(t.left, paths, res);
            // 回溯,意思就是包含t.left的路径已经全部收集过了,把t.left从path中删掉
            paths.remove(paths.size() - 1);
        }
        if (t.right != null) {
            // 左节点不是空
            traversal(t.right, paths, res);
            // 回溯,意思就是包含t.right的路径已经全部收集过了,把t.right从path中删掉
            paths.remove(paths.size() - 1);
        }
    }

递归 使用字符串

java 复制代码
public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();// 存最终的结果
        traversal(root, "", res);
        return res;
    }

    public void traversal(TreeNode t, String s, List<String> res) {
        if (t == null) {
            return;
        }
        // 前序遍历 中
        if (t.left == null && t.right == null) {
            // t是叶子节点,该收集结果了
            res.add(new StringBuilder(s).append(t.val).toString());
            return;
        }
        String tmp = new StringBuilder(s).append(t.val).append("->").toString();
        traversal(t.left, tmp, res);
        traversal(t.right, tmp, res);
        //结束 不需要回溯,因为传入tmp并不会改变tmp的值
    }

迭代法

java 复制代码
 public List<String> binaryTreePaths(TreeNode root) {
        //迭代法像层序遍历
        List<String> res = new ArrayList<>();// 存最终的结果
        if (root == null) {
            return res;
        }
        Deque<Object> stack = new ArrayDeque<>();

        // 节点和路径同时入栈
        stack.push(root);
        stack.push(root.val + "");//这样push的就是一个字符串
        while (!stack.isEmpty()) {
            // 节点和路径同时出栈
            String path = (String) stack.pop();
            TreeNode node = (TreeNode) stack.pop();
            // 若找到叶子节点
            if (node.left == null && node.right == null) {
                res.add(path);
            }
            // 右子节点不为空
            if (node.right != null) {
                stack.push(node.right);
                stack.push(path + "->" + node.right.val);
            }
            // 左子节点不为空
            if (node.left != null) {
                stack.push(node.left);
                stack.push(path + "->" + node.left.val);
            }
        }
        return res;
    }

迭代法像层序遍历

相关推荐
武藤一雄5 小时前
C# 关于多线程如何实现需要注意的问题(持续更新)
windows·后端·microsoft·c#·.net·.netcore·死锁
coding消烦员8 小时前
在 Windows 内网搭建 Git 仓库:共享普通仓库 vs 中心 bare 仓库
windows·git
xiaoliuliu1234512 小时前
IE8-WindowsXP-x86-CHS_23253_BDdl.exe 安装步骤(XP 32位简体中文版)
windows
百事牛科技12 小时前
文件不想再加密了?取消Word 打开密码的方法
windows·word
love530love14 小时前
EPGF 新手教程 13在 PyCharm(中文版 GUI)中创建 Hatch 项目环境,并把 Hatch 做成“项目自包含”(工具本地化为必做环节)
开发语言·ide·人工智能·windows·python·pycharm·hatch
峰上踏雪14 小时前
Go(Golang)Windows 环境配置关键点总结
开发语言·windows·golang·go语言
lusasky15 小时前
在Windows上编译、安装Rust
开发语言·windows·rust
麻辣长颈鹿Sir15 小时前
CMAKE指令集
linux·运维·windows·cmake·cmake指令集
Alice102915 小时前
如何在windows本地打包python镜像
开发语言·windows·python
北京流年16 小时前
windows安装jenkins并且编译app
运维·windows·jenkins