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"]

題意:

將一顆二叉樹的所有路徑打印出來

題解:

對這個二叉樹遞歸地進行左右子樹的歷遍,並記錄它們的路徑

package LeetCode.Easy;

import java.util.ArrayList;
import java.util.List;

import LeetCode.Dependencies.TreeNode;

/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
public class BinaryTreePaths {
    List<String> answer = new ArrayList<String>();
    public List<String> binaryTreePaths(TreeNode root) {
       
        if(root == null)
            return answer;
        
        //拜訪root節點
        findBinaryTreePaths(root, String.valueOf(root.val));

        return answer;
    }
    
    public void findBinaryTreePaths(TreeNode root, String path) {
        //若該節點的兩邊都沒有子節點時則表示到最終葉子節點了,則放入答案
        if(root.left == null && root.right == null)
            answer.add(path);
        //拜訪左邊
        if(root.left != null)
            findBinaryTreePaths(root.left, path + "->" + root.left.val);
        
        //拜訪右邊
        if(root.right != null)
            findBinaryTreePaths(root.right, path + "->" + root.right.val);
        
    }
}

Binary Tree Paths with Sum Equal to a Certain Value Score: 30 Author: Zhu Yungang Institution: Jilin University Given a binary tree where the nodes are non-zero integers, given an integer K, write a program to find all paths starting from the root node and ending at leaf nodes where the sum of the node values ​​equals K. For example, if K=15, for the binary tree t shown below, there are 2 paths that satisfy the condition: 8-5-2 and 8-7. If no path satisfies the condition, it should still be recognized. img.jpg Input Format: The input consists of two lines. The first line contains a set of space-separated integers, not exceeding 100, representing the preorder sequence of the binary tree with null pointer information (represented by 0). The second line is an integer K. Output Format: The first line of the output is an integer representing the number of paths that satisfy the condition; if no path satisfies the condition, output 0. Starting from the second line, each line represents a path that meets the condition. If multiple paths meet the condition, output them sequentially from left to right. Each node value in the path is followed by a space. If two different paths contain exactly the same node values, both must be output. Input Example 1: 8 5 1 0 0 2 0 0 7 0 0 15 Output Example 1: 2 8 5 2 8 7 Input Example 2: -1 2 0 0 3 0 0 2 Output Example 2: 1 -1 3 Input Example 3: 1 1 0 0 1 0 0 2 Output Example 3: 2 1 1 1 1 Input Example 4: -1 2 0 0 3 0 0 8 Output Example 4: 0C++
最新发布
11-08
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值