剑指offer
二叉树中和为某一值的路径
相似的企业真题
时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 64M,其他语言128M
热度指数:689455
本题知识点:
树
算法知识视频讲解
校招时部分企业笔试将禁止编程题跳出页面,为提前适应,练习时请使用在线自测,而非本地IDE。
题目描述
输入一颗二叉树的根节点和一个整数,按字典序打印出二叉树中结点值的和为输入整数的所有路径。路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径。
示例1
输入
复制
{10,5,12,4,7},22
{10,5,12,4,7},22
返回值
复制
[[10,5,7],[10,12]]
[[10,5,7],[10,12]]
示例2
输入
复制
{10,5,12,4,7},15
{10,5,12,4,7},15
返回值
复制
[]
[]
说明:本题目包含复杂数据结构TreeNode,
点此查看相关信息
上一题
下一题
登录
/
注册
我的提交
编辑器加载中...
import java.util.ArrayList; /** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { public ArrayList
> FindPath(TreeNode root,int target) { } }
/* struct TreeNode { int val; struct TreeNode *left; struct TreeNode *right; TreeNode(int x) : val(x), left(NULL), right(NULL) { } };*/ class Solution { public: vector
> FindPath(TreeNode* root,int expectNumber) { } };
# -*- coding:utf-8 -*- # class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None class Solution: # 返回二维列表,内部每个列表表示找到的路径 def FindPath(self, root, expectNumber): # write code here
using System.Collections.Generic; /* public class TreeNode { public int val; public TreeNode left; public TreeNode right; public TreeNode (int x) { val = x; } }*/ class Solution { public List
> FindPath(TreeNode root, int expectNumber) { // write code here } }
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function FindPath(root, expectNumber) { // write code here } module.exports = { FindPath : FindPath };
val = $val; } }*/ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ function FindPath( $root , $expectNumber ) { // write code here }
/* function TreeNode(x) { this.val = x; this.left = null; this.right = null; } */ function FindPath(root, expectNumber) { // write code here }
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ func FindPath( root *TreeNode , expectNumber int ) [][]int { // write code here }
# class TreeNode # attr_accessor :val, :left, :right # def initialize(val, left = nil, right = nil) # @val, @left, @right = val, left, right # end # end # # 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 # # @param root TreeNode类 # @param expectNumber int整型 # @return int整型二维数组 # class Solution def FindPath(root, expectNumber) # write code here end end
/** * class TreeNode(var val: Int) { * var left: TreeNode = null * var right: TreeNode = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ def FindPath(root: TreeNode,expectNumber: Int): Array[Array[Int]] = { // write code here } }
/** * class TreeNode(var `val`: Int) { * var left: TreeNode? = null * var right: TreeNode? = null * } */ object Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ fun FindPath(root: TreeNode?,expectNumber: Int): Array
{ // write code here } }
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * public TreeNode(int val) { * this.val = val; * } * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ public int[][] FindPath (TreeNode root, int expectNumber) { // write code here } }
/*class TreeNode { * val: number * left: TreeNode | null * right: TreeNode | null * constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { * this.val = (val===undefined ? 0 : val) * this.left = (left===undefined ? null : left) * this.right = (right===undefined ? null : right) * } * } */ /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ export function FindPath(root: TreeNode, expectNumber: number): number[][] { // write code here }
/** * public class TreeNode { * public var val: Int * public var left: TreeNode? * public var right: TreeNode? * public init(_ val: Int=0, _ left: TreeNode?=nil, _ right: TreeNode?=nil) { * self.val = val * self.left = left * self.right = right * } */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ func FindPath ( _ root: TreeNode?, _ expectNumber: Int) -> [[Int]] { // write code here } }
/** * #[derive(PartialEq, Eq, Debug, Clone)] * pub struct TreeNode { * pub val: i32, * pub left: Option
>, * pub right: Option
>, * } * * impl TreeNode { * #[inline] * fn new(val: i32) -> Self { * TreeNode { * val: val, * left: None, * right: None, * } * } * } */ struct Solution{ } impl Solution { fn new() -> Self { Solution{} } /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * @param root TreeNode类 * @param expectNumber int整型 * @return int整型二维数组 */ pub fn FindPath(&self, root: Option
>, expectNumber: i32) -> Vec
> { // write code here } }
{10,5,12,4,7},22
[[10,5,7],[10,12]]
{10,5,12,4,7},15
[]