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

迭代法像层序遍历

相关推荐
p***97614 小时前
从零开始在Windows系统上搭建一个node.js后端服务项目
windows·node.js
z***02609 小时前
【RabbitMQ】超详细Windows系统下RabbitMQ的安装配置
windows·分布式·rabbitmq
q***71019 小时前
从零开始在Windows系统上搭建一个node.js后端服务项目
windows·node.js
任子菲阳11 小时前
学Java第四十五天——不可变集合、Stream流
java·开发语言·windows
IDOlaoluo20 小时前
SQL Server 2022 企业版ISO安装步骤(附安装包从挂载到配置全流程)
windows
r***11331 天前
从零开始在Windows系统上搭建一个node.js后端服务项目
windows·node.js
Bruce_Liuxiaowei1 天前
Kali Linux 加入 Windows 域实战指南:解决域发现与加入失败问题
linux·运维·windows
鸽鸽程序猿1 天前
【项目】【抽奖系统】活动列表展示
windows
晚枫~2 天前
不同浏览器在Windows和Mac上的跨域配置方法
windows·macos
雪碧聊技术2 天前
怎么重启电脑的网卡(Windows系统)?
windows·重启网卡