算法刷题日志——二叉树10.31

二叉树的所有路径

在这里插入图片描述

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
            List<String> res = new ArrayList<>();
            paths(root,"",res);
            return res;
    }
    public void paths(TreeNode root,String path,List<String> res){
        if(root != null){
            StringBuffer sb = new StringBuffer(path);
            sb.append(Integer.toString(root.val));
            if(root.left==null&&root.right==null){
                res.add(sb.toString());
            }else{
                sb.append("->");
                paths(root.left,sb.toString(),res);
                paths(root.right,sb.toString(),res);
            }

        }
    }
}

相同的树

在这里插入图片描述

class Solution {
    public boolean compare(TreeNode tree1,TreeNode tree2){
        if(tree1==null&&tree2==null){
             return true;
        }
        if(tree1==null||tree2==null){
            return false;

        }
        if(tree1.val!=tree2.val)return false;
        boolean compareleft = compare(tree1.left,tree2.left);
        boolean compareright = compare(tree1.right,tree2.right);
        boolean isSame = compareleft&&compareright;
        return isSame;
    
    }
    public boolean isSameTree(TreeNode p, TreeNode q) {
        return compare(p,q);
    }
}

左叶子之和

在这里插入图片描述

BFS搜就完事了。

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root==null) return 0;
        Stack<TreeNode> stack = new Stack<>();
        stack.add(root);
        int ans=0;
        while(!stack.empty()){
            TreeNode   node =  stack.pop();
            if(node.left!=null){
                stack.add(node.left);
                if(node.left.left==null&&node.left.right==null){
                    ans+=node.left.val;
                }
            }
            if(node.right!=null){
                stack.add(node.right);
            }
        }
        return ans;
    }
}

找树左下角的值

在这里插入图片描述

使用 BFS 进行「层序遍历」,每次用当前层的首个节点来更新 ans,当 BFS 结束后,ans 存储的是最后一层最靠左的节点。

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        Deque<TreeNode> d = new ArrayDeque<>();
        d.addLast(root);
        int ans = 0;
        while (!d.isEmpty()) {
            int sz = d.size();
            ans = d.peek().val;
            while (sz-- > 0) {
                TreeNode poll = d.pollFirst();
                if (poll.left != null) d.addLast(poll.left);
                if (poll.right != null) d.addLast(poll.right);
            }
        }
        return ans;
    }
}

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

在这里插入图片描述

HashMap memo 需要一个哈希表来保存中序遍历序列中,元素和索引的位置关系。因为从后序序列中拿到根节点后,要在中序序列中查找对应的位置,从而将数组分为左子树和右子树;
postIndex:当前子树根节点在中序遍历数组中的索引位置;
left,right:当前子树在中序遍历数组的起始位置和结束位置标记;

在这里插入图片描述

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

最大二叉树

在这里插入图片描述

按题目递归就可以了

class Solution {
    public TreeNode constructMaximumBinaryTree(int[] nums) {
        return build(nums,0,nums.length-1);
    }
    public TreeNode build(int[] nums,int l,int r){
        if(l>r)return null;
        int idx = l;
        for(int i = l;i<=r;i++){
            if(nums[i]>nums[idx])idx=i;
        }
        TreeNode root = new TreeNode(nums[idx]);
        root.left=build(nums,l,idx-1);
        root.right=build(nums,idx+1,r);
        return root;
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值