Jan 12 - Binary Tree Paths; Tree; Recursion; DFS;

代码:

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String> list = new ArrayList<>();
        if(root == null) return list;
        String s = "";
        addTreePath(root, root.val, s, list);
        return list;
    }

    public void addTreePath(TreeNode root, int val, String s, List<String> list){
        if(root.left == null && root.right == null){
            list.add(s+root.val);
        }
        else if(root.left != null && root.right == null){
            addTreePath(root.left, val, s+root.val+"->", list);
        }
        else if(root.left == null && root.right != null){
            addTreePath(root.right, val, s+root.val+"->", list);
        }
        else{
            addTreePath(root.left, val, s+root.val+"->", list);
            addTreePath(root.right, val, s+root.val+"->", list);
        }
    }
}

  

时间: 2024-11-03 22:07:05

Jan 12 - Binary Tree Paths; Tree; Recursion; DFS;的相关文章

Jan 24 - Restore Ip Addresses; BackTracking; Recursion; DFS;

Code: public class Solution { public List<String> restoreIpAddresses(String s) { List<String> list = new ArrayList<>(); int len = s.length(); if(len < 4) return list; String ip = ""; validIpAddresses(s, ip, 4, len, 0, list);

Leetcode 257 Binary Tree Paths 二叉树 DFS

找到所有根到叶子的路径 深度优先搜索(DFS), 即二叉树的先序遍历. 1 /** 2 * Definition for a binary tree node. 3 * struct TreeNode { 4 * int val; 5 * TreeNode *left; 6 * TreeNode *right; 7 * TreeNode(int x) : val(x), left(NULL), right(NULL) {} 8 * }; 9 */ 10 class Solution { 11 p

Binary Tree Paths

输出二叉树的寻叶路径Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1  /   \2    3  \   5All root-to-leaf paths are: ["1->2->5", "1->3"] 遍历二叉树的时候,每当寻到叶结点,把路径装进结果里 1 package com.rust.data

[LeetCode][JavaScript]Binary Tree Paths

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"] https://leetcode.com/problems/binary-tree-paths/ 树的遍

LintCode Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Given the following binary tree: 1 1 2 / 3 2 3 4 5 5 All root-to-leaf paths are: 1 [ 2 "1->2->5", 3 "1->3" 4 ] As mentioned in the problem we want to get al

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-

&lt;LeetCode OJ&gt; 257. Binary Tree Paths

257. Binary Tree Paths My Submissions Question Total Accepted: 29282 Total Submissions: 113527 Difficulty: Easy 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: ["

[lintcode easy]Binary Tree Paths

Binary Tree Paths Given a binary tree, return all root-to-leaf paths. Example Given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: [ "1->2->5", "1->3" ] /** * Definition of TreeNode: * public class TreeNode {

leetCode 257. Binary Tree Paths 二叉树路径

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"] 思路: 1.采用二叉树的后序遍历非递归版 2.在叶子节点的时候处理字