代码随想录算法训练营第十八天| 513 找树左下角的值 112 路径总和 113 路径总和 || 106 从中序和后序遍历序列构造二叉树

目录

513 找树左下角的值

迭代

递归

112 路径总和

迭代

递归

113 路径总和 II

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

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



513 找树左下角的值

迭代

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        Deque<TreeNode> st = new LinkedList<>();
        st.add(root);
        while(!st.isEmpty()){
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.pop();
                if(i == 0)res = cur.val;
                if(cur.left != null)st.add(cur.left);
                if(cur.right != null)st.add(cur.right);
            }
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)

class Solution {
    public int findBottomLeftValue(TreeNode root) {
        int res = 0;
        Deque<TreeNode>st = new LinkedList<>();
        st.add(root);
        while(!st.isEmpty()){
            TreeNode cur = st.pop();
            if(cur.right != null)st.add(cur.right);
            if(cur.left != null)st.add(cur.left);//确保左边最后被遍历到
            res = cur.val; 
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)

递归

class Solution {
    private int value = 0;
    private int depth = 0;
    public int findBottomLeftValue(TreeNode root) {
        value = root.val;
        findLeftValue(root,0);
        return value;
    }
    private void findLeftValue(TreeNode root,int dep){
        if(root.left == null && root.right == null){
            if(dep > depth){
                depth = dep;
                value = root.val;
            }
        }
        if(root.left != null)findLeftValue(root.left,dep + 1);
        if(root.right != null)findLeftValue(root.right,dep + 1);
    }
}

时间复杂度O(n)

空间复杂度O(n)

112 路径总和

迭代

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null)return false;
        Deque<TreeNode>st1 = new LinkedList<>();
        Deque<Integer>st2 = new LinkedList<>();
        st1.add(root);
        st2.add(root.val);
        while(!st1.isEmpty()){
            int siz = st1.size();
            for(int i = 0;i < siz;i++){
                int sum = st2.pop();
                TreeNode cur = st1.pop();
                if(cur.left == null && cur.right == null && sum == targetSum)return true;
                if(cur.right != null){
                    st1.add(cur.right);
                    st2.add(cur.right.val + sum);
                }
                if(cur.left != null){
                    st1.add(cur.left);
                    st2.add(cur.left.val + sum);
                }
            }
        }
        return false;
    }
}

时间复杂度O(n)

空间复杂度O(n)

class Solution {
    public boolean hasPathSum(TreeNode root, int targetSum) {
        if(root == null)return false;
        Deque<TreeNode>st1 = new LinkedList<>();
        Deque<Integer>st2 = new LinkedList<>();
        st1.add(root);
        st2.add(root.val);
        while(!st1.isEmpty()){
            TreeNode cur = st1.pop();
            int sum = st2.pop();
            if(cur.left == null && cur.right == null && sum == targetSum)return true;
            if(cur.right != null){
                st1.add(cur.right);
                st2.add(cur.right.val + sum);
            }
            if(cur.left != null){
                st1.add(cur.left);
                st2.add(cur.left.val + sum);
            }
        }
        return false;
    }
}

时间复杂度O(n)

空间复杂度O(n)

递归

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

时间复杂度O(n)

空间复杂度O(n)

113 路径总和 II

class Solution {
        List<List<Integer>>res = new ArrayList<>();
        List<Integer>path = new LinkedList<>();
    public List<List<Integer>> pathSum(TreeNode root, int targetSum) {
        if(root == null)return res;
        dfs(root,targetSum);
        return res;
    }
    private void dfs(TreeNode root,int targetSum){
        path.add(root.val);
        if(root.left == null && root.right == null){
            if(root.val == targetSum){
                res.add(new ArrayList<>(path));
            }
            return;
        }
        if(root.left != null){
            dfs(root.left,targetSum - root.val);
            path.remove(path.size() - 1);
        }
        if(root.right != null){
            dfs(root.right,targetSum - root.val);
            path.remove(path.size() - 1);
        }
    }
}

时间复杂度O(n^2)

空间复杂度O(n)

class Solution {
    Map<Integer,Integer>mp;
    public TreeNode buildTree(int[] inorder, int[] postorder) {
        if(inorder.length == 0 || inorder == null)return null;
        mp = new HashMap<>();
        for(int i = 0;i < inorder.length;i++){
            mp.put(inorder[i],i);
        }
        return buildHelper(inorder,0,inorder.length,postorder,0,postorder.length);
    }
    private TreeNode buildHelper(int[] inorder,int inStart,int inEnd,int[] postorder,int postStart,int postEnd){
        //范围均为前闭后开
        if(postStart >= postEnd || inStart >= inEnd)return null;
        int mid = mp.get(postorder[postEnd - 1]);
        TreeNode cur = new TreeNode(inorder[mid]);
        int len = mid - inStart;
        cur.left = buildHelper(inorder,inStart,mid,postorder,postStart,len + postStart);
        cur.right = buildHelper(inorder,mid + 1,inEnd,postorder,len + postStart,postEnd - 1);
        return cur;
    }
}

时间复杂度O(n)

空间复杂度O(n)

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

class Solution {
    Map<Integer,Integer>mp;
    public TreeNode buildTree(int[] preorder, int[] inorder) {
        if(preorder == null || preorder.length == 0)return null;
        mp = new HashMap<>();
        for(int i = 0;i < inorder.length;i++){
            mp.put(inorder[i],i);
        }
        return buildHelper(preorder,0,preorder.length,inorder,0,inorder.length);
    }
    private TreeNode buildHelper(int[] preorder,int preStart,int preEnd,int[] inorder,int inStart,int inEnd){
        if(preStart >= preEnd || inStart >= inEnd)return null;
        int mid = mp.get(preorder[preStart]);
        TreeNode cur = new TreeNode(inorder[mid]);
        int prelen = mid - inStart;
        cur.left = buildHelper(preorder,preStart + 1,preStart + prelen + 1,inorder,inStart,mid);
        cur.right = buildHelper(preorder,preStart + prelen + 1,preEnd,inorder,mid + 1,inEnd);
        return cur;
    }
}

时间复杂度O(n)

空间复杂度O(n)

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

「已注销」

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值