随想录笔记-二叉树练习题

找树左下角的值

BFS

利用队列先进先出后进后出的特点

class Solution {
    public int findBottomLeftValue(TreeNode root) {
     if(root==null) return 0;
     Queue<TreeNode> queue=new LinkedList<TreeNode>();
    TreeNode node=root;
     queue.offer(root);
     while(!queue.isEmpty()){
        int len=queue.size();
        while(len>0){
            node=queue.poll();
            len--;

            if(node.right!=null) queue.offer(node.right);
            if(node.left!=null) queue.offer(node.left);

        }
     }
     return node.val;
    }
}

DFS

太妙了这个思路

513. 找树左下角的值 - 力扣(LeetCode)

class Solution {
    int max,ans;
    public int findBottomLeftValue(TreeNode root) {
        dfs(root,1);
        return ans;
    }

    public void dfs(TreeNode root,int depth){
        if(root==null) return ;
        if(depth>max){
            max=depth;
            ans=root.val;
        }
        dfs(root.left,depth+1);
        dfs(root.right,depth+1);
    }
  
}

路径总和

DFS

这个的思路我是仿照着二叉树的路径条数那个代码计算的

思路十分直接,就是计算每一条路径的总和放入到集合,到最后回到集合里面遍历

class Solution {
    List<Integer> res=new ArrayList<>();
    public boolean hasPathSum(TreeNode root, int targetSum) {
    int sum=0;
    deal(root,sum);
    for(Integer i:res){
        if(i.equals(targetSum))
        return true;
    }
   return false;
    }

    public void deal(TreeNode root,int sum){
        
        if(root==null) return ;

        sum+=root.val;

        if(root.left==null&&root.right==null)     res.add(sum);

        deal(root.left,sum);
        deal(root.right,sum);
    


    }
}

另一种DFS

class Solution {
    
    public boolean hasPathSum(TreeNode root, int targetSum) {
   if(root==null) return false;
   
   return dfs(root,targetSum,root.val);

    }
    public boolean dfs(TreeNode root,int targetSum,int pathSum){
     
     if(root==null) return false;

     if(root.left==null&&root.right==null){
        return pathSum==targetSum;
     }
     boolean left=false;
     boolean right=false;
     if(root.left!=null){
        left=dfs(root.left,targetSum,pathSum+root.left.val);
     }

    if(root.right!=null){
        right=dfs(root.right,targetSum,pathSum+root.right.val);
     }

     return left||right;

    }
}

 

初始递归

这个思路更加清楚,就是走一步就用目标值减去当前节点的值,当最后叶子节点的值是否与目标值相同

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;
    }

return hasPathSum(root.left,targetSum-root.val)||hasPathSum(root.right,targetSum-root.val);

    }
}

BFS

根本想不到

112. 路径总和 - 力扣(LeetCode)

  class Solution {
public boolean hasPathSum(TreeNode root, int sum) {
    // 如果根节点为空,直接返回false
    if (root == null) return false;
    // 创建一个队列,用于广度优先搜索(BFS)
    Deque<Pair<TreeNode, Integer>> queue = new LinkedList<>();
    // 将根节点和它的值作为一个对(Pair)添加到队列中
    queue.offer(new Pair<>(root, root.val));
    // 当队列不为空时,继续循环
    while (!queue.isEmpty()) {
        // 从队列中取出一个对
        Pair<TreeNode, Integer> pair = queue.poll();
        // 获取对中的节点和路径和
        TreeNode node = pair.getKey();
        int pathSum = pair.getValue();
        // 如果当前节点是叶子节点(即左右子节点都为空),并且路径和等于目标值,返回true
        if (node.left == null && node.right == null && pathSum == sum) {
            return true;
        }
        // 如果左子节点不为空,将左子节点和当前路径和加上左子节点的值作为一个对添加到队列中
        if (node.left != null) {
            queue.offer(new Pair<>(node.left, pathSum + node.left.val));
        }
        // 如果右子节点不为空,将右子节点和当前路径和加上右子节点的值作为一个对添加到队列中
        if (node.right != null) {
            queue.offer(new Pair<>(node.right, pathSum + node.right.val));
        }
    }
    // 如果遍历完所有节点都没有找到满足条件的路径,返回false
    return false;
}

路径总和II

113. 路径总和 II - 力扣(LeetCode)

class Solution {
    List<List<Integer>> res=new ArrayList<>();
    List<Integer> path=new ArrayList<>();
    
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        recur(root,targetSum);
        return res;
    
    }

    public void recur(TreeNode root, int targetSum){
        if(root==null) return ;
        path.add(root.val);
        targetSum-=root.val;
        if(targetSum==0&&root.left==null&&root.right==null){
            res.add(new LinkedList<Integer>(path));
        }
        recur(root.left,targetSum);
        recur(root.right,targetSum);
        path.removeLast();
    }
}

从中序和后序遍历数列构造二叉树

106. 从中序与后序遍历序列构造二叉树 - 力扣(LeetCode)

 

class Solution {
    HashMap<Integer,Integer> inorderArrayMap = new HashMap<>();
    int[] post;

    public TreeNode buildTree(int[] inorder, int[] postorder) {
        for(int i = 0;i < inorder.length; i++) {
            inorderArrayMap.put(inorder[i], i);//妙啊!将节点值及索引全部记录在哈希表中
        }
    
        post = postorder;
        TreeNode root = buildTree(0, inorder.length - 1, 0, post.length - 1);
        return root;
    }


    public TreeNode buildTree(int inorderStart, int inorderEnd, int postorderStart, int postorderEnd) {
        if(inorderEnd < inorderStart || postorderEnd < postorderStart) return null;

        int root = post[postorderEnd];//根据后序遍历结果,取得根节点
        int rootIndexInInorderArray = inorderArrayMap.get(root);//获取对应的索引

        TreeNode node = new TreeNode(root);//创建该节点
        node.left = buildTree(inorderStart, rootIndexInInorderArray - 1, postorderStart, postorderStart + rootIndexInInorderArray - inorderStart - 1);
        node.right = buildTree(rootIndexInInorderArray + 1, inorderEnd, postorderStart + rootIndexInInorderArray - inorderStart, postorderEnd - 1);
        return node;//注意!返回的是新建的node!
    }
}

最大二叉树

654. 最大二叉树 - 力扣(LeetCode)

利用数组和递归

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums, 0, nums.length - 1);
    }

    public TreeNode build(int[] nums, int startIndex, int endIndex) {
        if (startIndex > endIndex) return null;
        int index = maxElementIndex(nums, startIndex, endIndex);
        TreeNode newNode = new TreeNode(nums[index]);
        newNode.left = build(nums, startIndex, index - 1);
        newNode.right = build(nums, index + 1, endIndex);
        return newNode;
    }

    public int maxElementIndex(int[] nums, int startIndex, int endIndex) {
        int maxIndex = startIndex;
        for (int i = startIndex + 1; i <= endIndex; i++) {
            maxIndex = nums[maxIndex] < nums[i] ? i : maxIndex;
        }
        return maxIndex;
    }
}

我感觉栈的解法比较繁琐

所以就不做要求了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值