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

110.平衡二叉树

代码随想录讲解链接

思路

  • 什么是高度,什么是深度

代码

/**
 * 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 root){
        if(root == null){
            return 0;
        }
        TreeNode node = root;
        int leftHeight = getHeight(node.left);
        //左子树不平衡
        if(leftHeight == -1){
            return -1;
        }
        int rightHeight = getHeight(node.right);
        if(rightHeight == -1){
            return -1;
        }
        if(Math.abs(rightHeight - leftHeight) > 1){
            return -1;
        }
        ///  !!!!!!!!!!!!一开始写错
        return Math.max(leftHeight,rightHeight) + 1;
    }
}

257. 二叉树的所有路径

代码随想录讲解链接

思路

  • 回溯

代码

/**
 * 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 {
    List<String> res = new ArrayList<>();
    public List<String> binaryTreePaths(TreeNode root) {
        List<Integer> paths = new ArrayList<>();
        
        if(root == null){return res;}
        travelsal(root, paths);
        return res;
    }
    public List<String> travelsal(TreeNode root,List<Integer> paths){
        paths.add(root.val);
        if(root.left == null && root.right == null){
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < paths.size() - 1; i++){
                sb.append(paths.get(i)).append("->");
            }
            sb.append(paths.get(paths.size()-1));
            res.add(sb.toString());
            return res;
        }
        if(root.left != null){
            travelsal(root.left, paths);
            paths.remove(paths.size()-1);
        }
        if(root.right != null){
            travelsal(root.right, paths);
            paths.remove(paths.size()-1);
        }
        return res;
    }
}

404.左叶子之和

代码随想录讲解链接

思路

  • 递归三部曲
    • 确定递归函数的参数和返回值
      判断一个树的左叶子节点之和,那么一定要传入树的根节点,递归函数的返回值为数值之和
    • 确定终止条件
      如果遍历到空节点,那么左叶子值一定是0
      注意,只有当前遍历的节点是父节点,才能判断其子节点是不是左叶子。 所以如果当前遍历的节点是叶子节点,那其左叶子也必定是0,那么终止条件为:
      if (root == NULL) return 0;
      if (root->left == NULL && root->right== NULL) return 0; //其实这个也可以不写,如果不写不影响结果,但就会让递归多进行了一层。
    • 确定单层递归的逻辑
      当遇到左叶子节点的时候,记录数值,然后通过递归求取左子树左叶子之和,和 右子树左叶子之和,相加便是整个树的左叶子之和。

代码

/**
 * 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) {
        int res1 = sum1(root);
        return res1;
        
    }
    public int sum1(TreeNode root) {
        if(root == null){return 0;}
        Queue<TreeNode> que = new LinkedList<>();
        int res = 0;
        que.offer(root);
        while(!que.isEmpty()){
            int len = que.size();
            
            while(len > 0){
                TreeNode node = que.poll();//一开始写外面了
                if (node.left != null && node.left.left == null && node.left.right == null) {
                    
                    res += node.left.val;
                   //没有更新node的值 res += node.left.val;
                }
                if(node.left != null){
                    que.offer(node.left);
                }
                if(node.right != null){
                    que.offer(node.right);
                }
                len--;
            }
        }
        return res;
    }
}*/
class Solution {
    int mid = 0;
    public int sumOfLeftLeaves(TreeNode root) {
        if(root == null){return 0;}
        int leftv = sumOfLeftLeaves(root.left);
        int rightv = sumOfLeftLeaves(root.right);
        
        TreeNode node = root;
        if(node.left!=null && node.left.left == null && node.left.right == null){
            mid += node.left.val;
        } 
        return mid; 
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值