class Solution {
int maxDepth = -1;
int result = 0;
public int findBottomLeftValue(TreeNode root) {
return findBottomLeftValue(root, 0);
}
public int findBottomLeftValue(TreeNode root, int depth) {
if (root.left == null && root.right == null) {
if (depth > maxDepth) {
maxDepth = depth;
result = root.val;
}
}
if (root.left != null) {
depth++;
findBottomLeftValue(root.left, depth);
depth--;
}
if (root.right != null) {
depth++;
findBottomLeftValue(root.right, depth);
depth--;
}
return result;
}
}
class Solution {
public boolean hasPathSum(TreeNode root, int targetSum) {
if (root == null) {
return false;
}
targetSum -= root.val;
if (root.left == null && root.right == null) {
return targetSum == 0;
}
if (root.left != null) {
boolean b = hasPathSum(root.left, targetSum);
if (b) {
return true;
}
}
if (root.right != null) {
boolean b = hasPathSum(root.right, targetSum);
if (b) {
return true;
}
}
return false;
}
}
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if (postorder.length == 0) {
return null;
}
if (postorder.length == 1) {
return new TreeNode(postorder[postorder.length - 1]);
}
TreeNode root = new TreeNode(postorder[postorder.length - 1]);
int index =0 ;
for (int i = 0; i < inorder.length; i++) {
if (root.val == inorder[i]) {
index = i;
}
}
// 切中序
// 左中序
int[] leftInorder = Arrays.copyOfRange(inorder, 0, index);
// 右中序
int[] rightInorder = Arrays.copyOfRange(inorder, index + 1, inorder.length);
// 切后序
// 左后序
int[] leftPostorder = Arrays.copyOfRange(postorder, 0, leftInorder.length);
// 右后序
int[] rightPostOrder = Arrays.copyOfRange(postorder, leftInorder.length, postorder.length-1);
root.left = buildTree(leftInorder,leftPostorder);
root.right = buildTree(rightInorder, rightPostOrder);
return root;
}
}