● 513.找树左下角的值
层序遍历
class Solution {
public int findBottomLeftValue(TreeNode root) {
int result = 0;
ArrayDeque<TreeNode> que = new ArrayDeque<>();
que.offer(root);
while(!que.isEmpty()){
int size = que.size();
result = que.peek().val;
while(size-- > 0){
TreeNode temp = que.poll();
if(temp.left != null)
que.offer(temp.left);
if(temp.right != null)
que.offer(temp.right);
}
}
return result;
}
}
递归
class Solution {
private int maxDepth = -1;
private int result = 0;
public int findBottomLeftValue(TreeNode root) {
recrusion(root,0);
return result;
}
public void recrusion(TreeNode cur,int depth){
if(cur.left == null && cur.right == null && depth > maxDepth){
result = cur.val;
maxDepth = depth;
}
if(cur.left != null){
depth++;
recrusion(cur.left,depth);
depth--;
}
if(cur.right != null){
depth++;
recrusion(cur.right,depth);
depth--;
}
}
}
递归简化
java是值传递
传参数i + 1,不会改变i的值,隐式回溯
传参数i++,传进去的是i,方法执行结束后i+1,并不能实现回溯
传参数++i,传进去的是i+1,方法结束后i还是i+1,也没有实现回溯
class Solution {
private int maxDepth = -1;
private int result = 0;
public int findBottomLeftValue(TreeNode root) {
recrusion(root,0);
return result;
}
public void recrusion(TreeNode cur,int depth){
if(cur.left == null && cur.right == null && depth > maxDepth){
result = cur.val;
maxDepth = depth;
}
if(cur.left != null){
recrusion(cur.left,depth + 1);
}
if(cur.right != null){
recrusion(cur.right,depth + 1);
}
}
}
112. 路径总和
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;
}
}
113.路径总和ii
java是值传递,传递数组也是传递地址值,指向的地方一样
写算法时需注意:将path加入result中时,为防止list与result产生关联,需使用以下方法将list复制进result。这样就不会出现将list添加入result之后,若list再改变,则result也改变的现象。
result.add(new ArrayList<>(list));
不可直接:result.add(list);**
class Solution {
public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
List<List<Integer>> result = new ArrayList<>();
List<Integer> path = new ArrayList<>();
if(root == null)return result;
recrusion(root,targetSum,path,result);
return result;
}
public void recrusion(TreeNode cur,int targetSum,List<Integer>path,List<List<Integer>> result){
path.add(cur.val);
if(cur.left == null && cur.right == null){
if(targetSum - cur.val == 0)
result.add(new ArrayList<>(path));
else return;
}
if(cur.left != null){
recrusion(cur.left,targetSum - cur.val,path,result);
path.remove(path.size() - 1);
}
if(cur.right != null){
recrusion(cur.right,targetSum - cur.val,path,result);
path.remove(path.size() - 1);
}
}
}
106.从中序与后序遍历序列构造二叉树
流程
1、结束条件:后序数组为空
2、取后序数组最后一个节点
3、在中序数组里面找上面的节点
4、以该节点切割中序数组为左右数组
5、按照上面的切割后序数组
6、递归处理切好的左右区间
class Solution {
//需要有个属性来保存postorder供递归方法调用
private int[] post;
private Map<Integer,Integer> map = new HashMap<>();
public TreeNode buildTree(int[] inorder, int[] postorder) {
for(int i = 0;i < postorder.length;i++){
map.put(inorder[i],i);
}
post = postorder;
//取左闭右闭
int inorderStart = 0;
int inorderEnd = inorder.length - 1;
int postorderStart = 0;
int postorderEnd = postorder.length - 1;
TreeNode root = findTree(inorderStart,inorderEnd,postorderStart,postorderEnd);
return root;
}
public TreeNode findTree(int is,int ie,int ps,int pe){
//区间左闭右闭
if(ie < is || pe < ps)return null;
int val = post[pe];
//获取根节点在中序数组里面的位置
int positon = map.get(val);
TreeNode root = new TreeNode(val);
//中序和后序截取长度保持一致
root.left = findTree( is, positon -1, ps, ps + positon - 1 - is );
root.right = findTree( positon + 1,ie, ps + positon - is, pe -1);
return root;
}
}
105.从前序与中序遍历序列构造二叉树
class Solution {
private Map<Integer,Integer> map = new HashMap<>();
private int[] pre;
public TreeNode buildTree(int[] preorder, int[] inorder) {
pre = preorder;
for(int i = 0;i < inorder.length;i++)map.put(inorder[i],i);
int is = 0;
int ie = inorder.length - 1;
int ps = 0;
int pe = preorder.length - 1;
TreeNode root = findTree(is,ie,ps,pe);
return root;
}
private TreeNode findTree(int is,int ie,int ps,int pe){
if(ie < is || pe < ps)return null;
int val = pre[ps];
TreeNode root = new TreeNode(val);
int ri = map.get(val);
root.left = findTree(is,ri - 1,ps + 1,ps + ri - is);
root.right = findTree(ri + 1,ie,ps + ri - is + 1,pe);
return root;
}
}