代码随想录算法训练营第十七天| 110 平衡二叉树 257 二叉树的所有路径 404 左子叶之和

目录

110 平衡二叉树

 递归

迭代

 257 二叉树的所有路径

 递归

迭代

404 左子叶之和 

递归

迭代

层序遍历迭代法


110 平衡二叉树

求深度:前序遍历

求高度:后序遍历

 递归

class Solution {
    public boolean isBalanced(TreeNode root) {
        if(traversal(root) == -1)return false;
        return true;
    }
    //后序遍历求高度
    private int traversal(TreeNode root){
        if(root == null)return 0;
        int left = traversal(root.left);
        if(left == -1)return -1;
        int right = traversal(root.right);
        if(right == -1)return -1;
        if(Math.abs(left - right) > 1)return -1;
        return 1 + Math.max(left,right);//返回整个子树的最大值
    }
}

 时间复杂度O(n)

空间复杂度O(n)

迭代

class Solution {
     public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }
        Stack<TreeNode> stack = new Stack<>();
        TreeNode pre = null;
        while (root!= null || !stack.isEmpty()) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            TreeNode cur = stack.peek();
            // 右结点为null或已经遍历过
            if (cur.right == null || cur.right == pre) {
                // 比较左右子树的高度差,输出
                if (Math.abs(getHeight(cur.left) - getHeight(cur.right)) > 1) {
                    return false;
                }
                stack.pop();
                pre = cur;
                root = null;// 当前结点下,没有要遍历的结点了
            } else {
                root = cur.right;// 右结点还没遍历,遍历右结点
            }
        }
        return true;
    }
    public int getHeight(TreeNode root) {
        if(root == null)return 0;
        Deque<TreeNode>st = new LinkedList<>();
        int depth = 0;
        st.offer(root);
        while(!st.isEmpty()){
            int siz = st.size();
            depth++;
            for(int i = 0;i < siz;i++){
                TreeNode cur = st.poll();
                if(cur.left != null)st.offer(cur.left);
                if(cur.right != null)st.offer(cur.right);
            }
        }
        return depth;
    }
}

 时间复杂度O(n^2)

空间复杂度O(n^2)

 257 二叉树的所有路径

 递归

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String>res = new ArrayList<>();
        List<Integer>nums = new ArrayList<>();
        traversal(root,res,nums);
        return res;
    }
    private void traversal(TreeNode root,List<String>res,List<Integer>nums){
        //前序遍历 中左右
        nums.add(root.val);
        //达到子节点,将结果加入到res中
        if(root.left == null && root.right == null){
            StringBuilder sb = new StringBuilder();
            for(int i = 0;i < nums.size() - 1;i++){
                sb.append(nums.get(i)).append("->");
            }
            sb.append(nums.get(nums.size() - 1));
            res.add(sb.toString());
            return;
        }
        //左
        if(root.left != null){
            traversal(root.left,res,nums);//递归
            nums.remove(nums.size() - 1);//回溯
        }
        //右
        if(root.right != null){
            traversal(root.right,res,nums);
            nums.remove(nums.size() - 1);
        }
    }
}

时间复杂度O(n^2) 

空间复杂度O(n^2)

迭代

class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<String>res = new ArrayList<>();
        if(root == null)return res;
        Deque<Object>st = new LinkedList<>();
        st.push(root);
        st.push(root.val + "");
        while(!st.isEmpty()){
            String str = (String)st.poll();
            TreeNode cur = (TreeNode)st.poll();
            if(cur.left == null && cur.right == null){
                res.add(str);
                continue;
            }
            if(cur.left != null){
                st.push(cur.left);
                st.push(str + "->" + cur.left.val);
            }
            if(cur.right != null){
                st.push(cur.right);
                st.push(str + "->" + cur.right.val);
            }
        }
        return res;
    }
}

时间复杂度O(n^2) 

空间复杂度O(n^2)

404 左子叶之和 

递归

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        return traversal(root);
    }
    private int traversal(TreeNode root){
        if(root == null)return 0;
        if(root.left == null && root.right == null)return 0;
        int sum = 0;
        if(root.left != null && root.left.left == null && root.left.right == null)sum += root.left.val;//如果该子树下有左叶子节点,将其val加入到sum中
        sum += traversal(root.left);//加上其左子树下的左叶子之和
        sum += traversal(root.right);//加上其右子树下的左叶子节点之和
        return sum;
    }
}

时间复杂度O(n)

空间复杂度O(n)为所开栈空间的大小,最坏情况下二叉树呈链状,此时为O(n) 

迭代

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null)return 0;
        Deque<TreeNode>st = new LinkedList<>();
        st.push(root);
        int res = 0;
        while(!st.isEmpty()){
            TreeNode node = st.pop();
            if(node.left != null && node.left.left == null && node.left.right == null)res += node.left.val;
            if(node.right != null)st.add(node.right);
            if(node.left != null)st.add(node.left);
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)

层序遍历迭代法

class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null)return 0;
        Deque<TreeNode>st = new LinkedList<>();
        st.push(root);
        int res = 0;
        while(!st.isEmpty()){
            int siz = st.size();
            for(int i = 0;i < siz;i++){
                TreeNode node = st.poll();
                if(node.left != null){
                    st.push(node.left);
                    if(node.left.left == null && node.left.right == null)res += node.left.val;
                }
                if(node.right != null){
                    st.push(node.right);
                }
            }
        }
        return res;
    }
}

时间复杂度O(n)

空间复杂度O(n)

  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

「已注销」

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

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

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

打赏作者

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

抵扣说明:

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

余额充值