资讯专栏INFORMATION COLUMN

Leetcode[257] Binary Tree Paths

liujs / 1211人阅读

LeetCode[257] Binary Tree Paths

Given a binary tree, return all root-to-leaf paths.

For example, given the following binary tree:

   1
 /   
2     3
 
  5

All root-to-leaf paths are:
["1->2->5", "1->3"]

Recursion

复杂度
O(N), O(H)

思路
基本算法,递归。

代码

public List binaryTreePaths(TreeNode root) {
    List list = new LinkedList<>();
    helper(root, "", list);
    return list;
}

public void helper(TreeNode root, String cur, List list) {
    if(root == null) return null;
    cur += root.val;
    if(root.left == null && root.right == null) {
        list.add(cur);
        return;
    }
    cur += "->";
    helper(root.left, cur, list);
    helper(root.right, cur, list);
}
Iteration

复杂度
O(N), O(N)

思路
递归用stack和stack进行dfs。

代码

public List paths(TreeNode root) {
    StringBuilder builder = new StringBuilder();
    List res = new LinkedList<>();
    if(root == null) return res;
    Stack stack = new Stack<>();
    Set set = new HashSet<>();
    stack.push(root);
    builder.append(root.val);
    while(!stack.isEmpty()) {
        TreeNode cur = stack.peek();
        set.add(cur);
        if(cur.left != null && !set.contains(cur.left)) {
            builder.append(cur.left.val);
            stack.push(cur.left);
            continue;
        }
        if(cur.right != null && !set.contains(cur.right)) {
            builder.append(cur.right.val);
            stack.push(cur.right);
            continue;
        }
        res.add(builder.toString());
        builder.deleteCharAt(builder.length() - 1);
    }
    return res;
}

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/65270.html

相关文章

  • 前端 | 每天一个 LeetCode

    摘要:在线网站地址我的微信公众号完整题目列表从年月日起,每天更新一题,顺序从易到难,目前已更新个题。这是项目地址欢迎一起交流学习。 这篇文章记录我练习的 LeetCode 题目,语言 JavaScript。 在线网站:https://cattle.w3fun.com GitHub 地址:https://github.com/swpuLeo/ca...我的微信公众号: showImg(htt...

    张汉庆 评论0 收藏0
  • LeetCode 攻略 - 2019 年 7 月下半月汇总(100 题攻略)

    摘要:月下半旬攻略道题,目前已攻略题。目前简单难度攻略已经到题,所以后面会调整自己,在刷算法与数据结构的同时,攻略中等难度的题目。 Create by jsliang on 2019-07-30 16:15:37 Recently revised in 2019-07-30 17:04:20 7 月下半旬攻略 45 道题,目前已攻略 100 题。 一 目录 不折腾的前端,和咸鱼有什么区别...

    tain335 评论0 收藏0
  • [Leetcode] Binary Tree Paths 二叉树路径

    摘要:递归法复杂度时间空间递归栈空间对于二叉树思路简单的二叉树遍历,遍历的过程中记录之前的路径,一旦遍历到叶子节点便将该路径加入结果中。当遇到最小公共祖先的时候便合并路径。需要注意的是,我们要单独处理目标节点自身是最小公共祖先的情况。 Root To Leaf Binary Tree Paths Given a binary tree, return all root-to-leaf pat...

    Vicky 评论0 收藏0
  • [Leetcode-Tree] Path Sum I II III

    摘要:解题思路利用递归,对于每个根节点,只要左子树和右子树中有一个满足,就返回每次访问一个节点,就将该节点的作为新的进行下一层的判断。代码解题思路本题的不同点是可以不从开始,不到结束。代码当前节点开始当前节点左节点开始当前节点右节点开始 Path SumGiven a binary tree and a sum, determine if the tree has a root-to-lea...

    notebin 评论0 收藏0
  • [LeetCode] Path Sum (I & II & III)

    摘要: 112. Path Sum Problem Given a binary tree and a sum, determine if the tree has a root-to-leaf path such that adding up all the values along the path equals the given sum. Note: A leaf is a node...

    张金宝 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<