import Tree , { TreeNode } from './BinaryTree' ; param { TreeNode } root param { number } sum return { boolean } function RouteSum ( root , sum ) { const getSum = ( root , sum , total ) : boolean => { if ( ! root . left && ! root . right ) { total += root . val ; if ( total === sum ) { return true ; } else { return false ; } } else { if ( root . left && ! root . right ) { return getSum ( root . left , sum , total + root . val ) ; } else if ( root . right && ! root . left ) { return getSum ( root . right , sum , total + root . val ) ; } else { return ( getSum ( root . left , sum , total + root . val ) || getSum ( root . right , sum , total + root . val ) ) ; } } } ; if ( ! root || root . val === undefined ) { return false ; } return getSum ( root , sum , 0 ) ; } param { TreeNode } root param { number } sum return { number [ ] [ ] } function RouteSumWithRoute ( root , sum ) { let result = [ ] ; const getSumRoute = ( root , sum , total , array = [ ] ) => { if ( ! root . left && ! root . right ) { total += root . val ; if ( total === sum ) { array . push ( root . val ) ; result . push ( array ) ; } } else { array . push ( root . val ) ; if ( root . left && ! root . right ) { getSumRoute ( root . left , sum , total + root . val , [ ... array ] ) ; } else if ( root . right && ! root . left ) { getSumRoute ( root . right , sum , total + root . val , [ ... array ] ) ; } else { getSumRoute ( root . left , sum , total + root . val , [ ... array ] ) ; getSumRoute ( root . right , sum , total + root . val , [ ... array ] ) ; } } } ; if ( ! root ) { return [ ] ; } getSumRoute ( root , sum , 0 ) ; return result ; } export { RouteSum , RouteSumWithRoute } ;