算法Day13 | Leetcode110.平衡二叉树、257. 二叉树的所有路径、404.左叶子之和、222.完全二叉树的节点个数

110.平衡二叉树

力扣链接

思路

后序遍历,计算到该节点的最大高度,判断左右子树相减是否大于1,接收子树的判断结果,如果false直接返回不用做判断了

代码

/**
 * 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) {
        int result=charge(root);
        if(result==-1) return false;
        return true;
    }

    public int charge(TreeNode root){
        if(root==null) return 0;
        int leftdepth=charge(root.left);
        if(leftdepth==-1) return -1;
        int rightdepth=charge(root.right);
        if(rightdepth==-1) return -1;
        if(Math.abs(leftdepth-rightdepth)>1) return -1;
        return Math.max(leftdepth,rightdepth)+1;
    }
}

257. 二叉树的所有路径

力扣链接

思路

前序遍历,利用List存储路径(方便回退),进来就存路径里,我们找到的路径不包含空节点,所以要对终止条件做限制。当到达终止条件时,将路径存到结果集里。

代码

/**
 * 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<String> result=new ArrayList<>();
        if(root==null) return result;
        List<Integer> path=new ArrayList<>();
        find(root,path,result);
        return result;
    }

    public void find(TreeNode node,List<Integer> path,List<String> result){
        path.add(node.val);
        if(node.left==null&&node.right==null){
            String s="";
            for(int i=0;i<path.size()-1;i++){
                s+=path.get(i)+"->";
            }
            s+=path.get(path.size()-1);     //i在离开循环后就被回收了
            result.add(s);
        }

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

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

404.左叶子之和

力扣链接

思路

这里不能用层序遍历,因为最左边的节点不一定是叶子节点。后序遍历,当左节点不为空,并且左节点为叶子节点时,值加入到总和中

代码

/**
 * 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 sum=0;
        return find(root,0);
    }

    public int find(TreeNode node,int sum){
        if(node==null) return 0;
        int sum1=find(node.left,sum);
        int sum2=find(node.right,sum);
        if(node.left!=null&&node.left.left==null&&node.left.right==null)  sum+=node.left.val;
        return sum+=sum1+sum2;
    }
}

222.完全二叉树的节点个数

力扣链接

思路

层序遍历或者后序遍历都可以

代码

/**
 * 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 countNodes(TreeNode root) {
        if(root==null) return 0;
        int count1=countNodes(root.left);
        int count2=countNodes(root.right);
        return count1+count2+1;
    }
}
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值