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

迭代法像层序遍历

相关推荐
s9123601012 小时前
Rust Ubuntu下编译生成环境win程序踩坑指南
windows·ubuntu·rust
智想天开3 小时前
14.外观模式:思考与解读
windows·microsoft·设计模式·外观模式
悟能不能悟3 小时前
windows如何使用cmd命令翻转屏幕
windows
晚上不睡觉的周某人4 小时前
Java应用7(类集)
java·开发语言·windows
海天鹰5 小时前
Catsxp:STATUS_BREAKPOINT
windows
只可远观14 小时前
Flutter 泛型 泛型方法 泛型类 泛型接口
服务器·windows·flutter
葡萄学妹15 小时前
Windows server:
windows
zhishishe15 小时前
如何修复卡在恢复模式下的 iPhone:简短指南
windows·macos·ios·objective-c·cocoa·iphone
Icoolkj16 小时前
在 Windows 系统上升级 Node.js
windows·node.js
长沙火山18 小时前
9.ArkUI List的介绍和使用
数据结构·windows·list