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;
    }

迭代法像层序遍历

相关推荐
r***99824 小时前
mysql9.0windows安装
windows·adb
OneLIMS5 小时前
Windows Server 2022 + IIS + ASP.NET Core 完整可上传大文件的 报错的问题
windows·后端·asp.net
笨鸟要努力6 小时前
Qt C++ windows 设置系统时间
c++·windows·qt
q***44156 小时前
SQLMAP的下载安装和使用(Windows)
windows
子燕若水8 小时前
Nuitka 打包 教程windows版本
windows
吴声子夜歌9 小时前
Windows——注册表
windows
i***118610 小时前
Windows环境下安装Redis并设置Redis开机自启
数据库·windows·redis
5***g29812 小时前
Windows安装Rust环境(详细教程)
开发语言·windows·rust
吴声子夜歌12 小时前
Windows——PowerShell
windows
极简之美13 小时前
Mac 远程连接 Windows 简明教程(2025 实测版)
windows·macos