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

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

110. 平衡二叉树

  1. 左右子树高度差不大于1 就是平衡二叉树
  2. 递归三部曲 终止条件 node==null 高度为0 return 0
  3. 向左遍历 求得左边子树的高度 leftheight = getheight(node.left)
  4. 向右遍历 求得右边子树的高度 rightheight
  5. 若判断出来左右子树高度相差>1了 返回-1 就一直往上传
  6. 然后进行中的操作 进行高度计算 若>1 return -1 若小于1 就return res
  7. 总结 求高度都用 后序遍历 因为要先处理完下面的子节点 才能计算当前高度
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public boolean isBalanced(TreeNode root) {
        return getheight(root)!=-1;

    }

    public int getheight(TreeNode node){
        if(node == null) return 0;

        int leftheight = getheight(node.left);
        int rightheight = getheight(node.right);

        if(leftheight==-1) return -1;
        if(rightheight==-1) return -1;

        int res = Math.abs(rightheight-leftheight);
        if(res>1){
            return -1;
        }else{
            return Math.max(rightheight,leftheight)+1;
        }

        
    }
}

257. 二叉树的所有路径

  1. 此题从上往下遍历 用前序遍历 首先用中存储节点 然后判断终止条件 若到叶节点就停止遍历 把该条路径加入result
  2. 然后去遍历左右节点 注意回溯 即 从底往上时 要弹出当前元素
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public List<String> binaryTreePaths(TreeNode root) {
        List<Integer> path = new ArrayList<>();
        List<String> result = new ArrayList<>();
        if(root==null) return result;
        
        return getroute(root,path,result);

    }

    public List<String> getroute(TreeNode node,List<Integer> path,List<String> result){
        //前序递归遍历 
        //先中操作加入节点 因为不然终止时 没有加入叶节点
        path.add(node.val);
        //终止条件
        if(node.left==null && node.right==null){
            //将path的结果装入 result
            //将path中的数字 转成string ->
            StringBuilder sb = new StringBuilder();
            for(int i = 0;i<path.size()-1;i++){
                sb.append(path.get(i)).append("->");

            }
            sb.append(path.get(path.size()-1));
            result.add(sb.toString());
            return result;
        }
        //中左右
        //为了防止出现空指针异常 
        if(node.left!=null){
            getroute(node.left,path,result);
            //同时需要从底网上回溯时  弹出当前值
            path.remove(path.size()-1);
        }

        if(node.right!=null){
            getroute(node.right,path,result);
            path.remove(path.size()-1);
        }

        return result;

    }
}

404. 左叶子之和

  1. 首先左叶子 的定义 是叶节点 即没有左右子节点的节点 其次 他还得是 其父节点左边的节点
  2. 判断终结条件的时候 当到空节点 return 0 当到 叶节点 也return 0 因为是后序遍历 叶节点如果是左节点的话 他的值已经被他的父节点统计过了
  3. 左右遍历时 分别都 统计左右子树 的目前左右子树 左叶节点的和
  4. 然后中的操作为 判断当下一个左节点为左叶节点时 就取他的值 然后加到 该节点的左右子树 左叶节点和中
/**
 * Definition for a binary tree node.
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode() {}
 *     TreeNode(int val) { this.val = val; }
 *     TreeNode(int val, TreeNode left, TreeNode right) {
 *         this.val = val;
 *         this.left = left;
 *         this.right = right;
 *     }
 * }
 */
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        return leftleavesSum(root);
        

    }
    //后序遍历
    public int leftleavesSum(TreeNode node){
        if(node==null) return 0;
        if(node.left==null && node.right==null) return 0;

        int leftsum = leftleavesSum(node.left);
        int rightsum = leftleavesSum(node.right);
        int midnum = 0;

        //中
        if(node.left!=null && node.left.left==null && node.left.right==null){
            midnum = node.left.val;
        }

        int sum = midnum + leftsum + rightsum;
        return sum;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值