找树左下角的值
思路:这里寻找最左下角的值,其实用前中后序都是可以的,只要保证第一遍历的是左边开始就可以。设置Deep记录遍历的最大深度,deep记录当前深度。当遇到叶子节点时而且当前深度比最大深度还大则更换最大深度为deep并存储当前节点的值(这个时候说明遇到的就是当前深度下最左边的叶子节点,但不一定是最最大深度的最左边的叶子节点,还要继续往后遍历)。最后value存储的结果,就是最大深度下最左下角的值了
代码:
class Solution {
private int Deep = -1;
private int value = 0;
public int findBottomLeftValue(TreeNode root) {
value = root.val;
findLeftValue(root,0);
return value;
}
private void findLeftValue (TreeNode root,int deep) {
if (root == null) return;
if (root.left == null && root.right == null) {
if (deep > Deep) {
value = root.val;
Deep = deep;
}
}
if (root.left != null) findLeftValue(root.left,deep + 1);
if (root.right != null) findLeftValue(root.right,deep + 1);
}
}
路径总和
思路1:其实这里的用前中后序都是可以的,重要的是回溯的过程。每次遍历节点,就把target值减去当前节点值,然后判断是否为叶子节点,如果是叶子节点就直接返回true,因为题目意思就是遇到一条路径等于target值就直接返回即可。当不是叶子节点且节点不为空,就继续遍历节点值,直到遇到一条路径等于target就一直返回true到根节点,否则就是返回false
代码1:
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 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;
}
}
思路2:
这题和题目1其实思路一样,只是不是直接返回true,而是把这条路径的值全部存起来,最后把所有等于target值的路径输出
代码2:
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> res = new ArrayList<>();
if (root == null) return res;
List<Integer> path = new LinkedList<>();
preorderdfs(root, targetSum, res, path);
return res;
}
public void preorderdfs(TreeNode root, int targetsum, List<List<Integer>> res, List<Integer> path) {
path.add(root.val);
// 遇到了叶子节点
if (root.left == null && root.right == null) {
// 找到了和为 targetsum 的路径
if (targetsum - root.val == 0) {
res.add(new ArrayList<>(path));
}
return; // 如果和不为 targetsum,返回
}
if (root.left != null) {
preorderdfs(root.left, targetsum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
if (root.right != null) {
preorderdfs(root.right, targetsum - root.val, res, path);
path.remove(path.size() - 1); // 回溯
}
}
}
从中序与后序遍历序列构造二叉树
思路:其实这道题就是理解二叉树的一个过程,构建二叉树,首先得知道前序中序或中序后序,知道前序后序是不能构造二叉树的。以后序中序为例;这里重要的是找到根节点以及找到根节点后如何分割这个后序中序数组。后序数组中,最后一个元素就是根节点,然后通过这个根节点的值,找到在对应中序的下标(index);找到下标之后,就是分割后序中序数组,通过index找到左中序右中序,以及左后序和右后序,重点注意右中序,因为涉及数组溢出和超出数组范围的情况(主要是因为在中序数组中间会出现要构建子树的情况);得到分割后的数组,就继续调用构建树的方法即可
代码:
class Solution {
public TreeNode buildTree(int[] inorder, int[] postorder) {
if(postorder.length == 0 || inorder.length == 0)
return null;
return buildHelper(inorder, 0, inorder.length, postorder, 0, postorder.length);
}
private TreeNode buildHelper(int[] inorder, int inorderStart, int inorderEnd, int[] postorder, int postorderStart, int postorderEnd){
if(postorderStart == postorderEnd)
return null;
int rootVal = postorder[postorderEnd - 1];
TreeNode root = new TreeNode(rootVal);
int middleIndex;
for (middleIndex = inorderStart; middleIndex < inorderEnd; middleIndex++){
if(inorder[middleIndex] == rootVal)
break;
}
int leftInorderStart = inorderStart;
int leftInorderEnd = middleIndex;
int rightInorderStart = middleIndex + 1;
int rightInorderEnd = inorderEnd;
int leftPostorderStart = postorderStart;
int leftPostorderEnd = postorderStart + (middleIndex - inorderStart);//这个是为了防止数组溢出,因为有可能中序数组中间部分是要构建树的,所以postorderStart和inorderStart可能不为零的情况,所以要减去
int rightPostorderStart = leftPostorderEnd;
int rightPostorderEnd = postorderEnd - 1;
root.left = buildHelper(inorder, leftInorderStart, leftInorderEnd, postorder, leftPostorderStart, leftPostorderEnd);
root.right = buildHelper(inorder, rightInorderStart, rightInorderEnd, postorder, rightPostorderStart, rightPostorderEnd);
return root;
}
}