力扣513 找树左下角的值
class Solution { public int findBottomLeftValue ( TreeNode root) { Queue < TreeNode > que = new LinkedList < > ( ) ; que. offer ( root) ; int res = 0 ; while ( ! que. isEmpty ( ) ) { int size = que. size ( ) ; for ( int i = 0 ; i < size; i++ ) { TreeNode tmp = que. poll ( ) ; if ( i == 0 ) res = tmp. val; if ( tmp. left != null ) que. offer ( tmp. left) ; if ( tmp. right != null ) que. offer ( tmp. right) ; } } return res; }
}
力扣112: 路径总和
class Solution { public boolean hasPathSum ( TreeNode root, int targetSum) { if ( root == null ) return false ; if ( root. left == null && root. right == null ) return ( root. val - targetSum == 0 ) ; targetSum -= root. val; if ( root. left != null ) { boolean left = hasPathSum ( root. left, targetSum) ; if ( left) return true ; } if ( root. right != null ) { boolean right = hasPathSum ( root. right, targetSum) ; if ( right) return true ; } return false ; }
}
力扣113: 路径总和II
class Solution { public List < List < Integer > > pathSum ( TreeNode root, int targetSum) { List < List < Integer > > res = new ArrayList < > ( ) ; List < Integer > count = new ArrayList < > ( ) ; traversalSum ( root, targetSum, res, count) ; return res; } public void traversalSum ( TreeNode root, int targetSum, List < List < Integer > > res, List < Integer > count) { if ( root == null ) return ; count. add ( root. val) ; if ( root. left == null && root. right == null && root. val == targetSum) { res. add ( new ArrayList < > ( count) ) ; } if ( root. left != null ) traversalSum ( root. left, targetSum - root. val, res, count) ; if ( root. right != null ) traversalSum ( root. right, targetSum - root. val, res, count) ; count. remove ( count. size ( ) - 1 ) ; }
}
力扣106.从中序与后序遍历序列构造二叉树
class Solution { public TreeNode buildTree ( int [ ] inorder, int [ ] postorder) { if ( inorder == null || inorder. length == 0 || postorder == null || postorder. length == 0 ) { return null ; } return traversal ( inorder, 0 , inorder. length - 1 , postorder, 0 , postorder. length - 1 ) ; } public TreeNode traversal ( int [ ] inorder, int inStart, int inEnd, int [ ] postorder, int postStart, int postEnd) { if ( postStart > postEnd) return null ; TreeNode root = new TreeNode ( ) ; root. val = postorder[ postEnd] ; if ( postStart == postEnd) return root; int index; for ( index = inStart; index < inEnd; index++ ) { if ( inorder[ index] == root. val) break ; } int inStartLeft = inStart; int inEndLeft = index - 1 ; int inStartRight = index + 1 ; int inEndRight = inEnd; int postStartLeft = postStart; int postEndLeft = postStart + ( index - inStart) - 1 ; int postStartRight = postEndLeft + 1 ; int postEndRight = postEnd - 1 ; root. left = traversal ( inorder, inStartLeft, inEndLeft, postorder, postStartLeft, postEndLeft) ; root. right = traversal ( inorder, inStartRight, inEndRight, postorder, postStartRight, postEndRight) ; return root; } }
力扣105.从前序与中序遍历序列构造二叉树
class Solution { public TreeNode buildTree ( int [ ] preorder, int [ ] inorder) { if ( preorder == null || preorder. length == 0 || inorder == null || inorder. length == 0 ) { return null ; } return traversal ( preorder, 0 , preorder. length - 1 , inorder, 0 , inorder. length - 1 ) ; } public TreeNode traversal ( int [ ] preorder, int preStart, int preEnd, int [ ] inorder, int inStart, int inEnd) { if ( preStart > preEnd) return null ; TreeNode root = new TreeNode ( ) ; root. val = preorder[ preStart] ; if ( preStart == preEnd) return root; int index; for ( index = inStart; index <= inEnd; index++ ) { if ( inorder[ index] == root. val) break ; } int inStartLeft = inStart; int inEndLeft = index - 1 ; int inStartRight = index + 1 ; int inEndRight = inEnd; int preStartLeft = preStart + 1 ; int preEndLeft = preStartLeft + ( index - inStart) - 1 ; int preStartRight = preEndLeft + 1 ; int preEndRight = preEnd; root. left = traversal ( preorder, preStartLeft, preEndLeft, inorder, inStartLeft, inEndLeft) ; root. right = traversal ( preorder, preStartRight, preEndRight, inorder, inStartRight, inEndRight) ; return root; }
}