2021-09-07

平衡二叉树
class Solution {
    public boolean isBalanced(TreeNode root) {
        if(root==null) return true;
        if(Math.abs(cal(root.left)-cal(root.right))>1)
        {
            return false;
        }
        return isBalanced(root.left)&&isBalanced(root.right);
    }
    public int cal(TreeNode root)
    {
        if(root==null) return 0;
        return Math.max(cal(root.left)+1,cal(root.right)+1);
    }
}

二叉树最底层最左边的值
class Solution {
    public int findBottomLeftValue(TreeNode root) {
        if(root==null) return 0;
        List<List<Integer>> sum=level(root);
        int t=0;
        for(List<Integer> x : sum)
        {
            for(int y : x)
            {
                t=y;
                return y;
            }
        }
        return t;
    }
    public List<List<Integer>> level(TreeNode root)
    {
        List<List<Integer>> sum=new LinkedList<>();
        if(root==null) return sum;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty())
        {
            TreeNode temp;
            List<Integer> x=new ArrayList<>();
            int t=queue.size();
            for(int i=0;i<t;i++)
            {
                temp=queue.poll();
                x.add(temp.val);
                if(temp.left!=null) queue.add(temp.left);
                if(temp.right!=null) queue.add(temp.right);
            }
            sum.add(x);
        }
        Collections.reverse(sum);
        return sum;
    }
}

二叉树的右侧视图
class Solution {
    List<Integer> x=new LinkedList<>();
    public List<Integer> rightSideView(TreeNode root) {
        if(root==null) return x;
        List<List<Integer>> sum=level(root);
        int y=0;
        for(List<Integer> m : sum)
        {
            for(int n : m)
            {
                y=n;
            }
            x.add(y);
        }
        return x;
    }
     public List<List<Integer>> level(TreeNode root)
    {
        List<List<Integer>> sum=new LinkedList<>();
        if(root==null) return sum;
        Queue<TreeNode> queue=new LinkedList<>();
        queue.add(root);
        while(!queue.isEmpty())
        {
            List<Integer> x=new LinkedList<>();
            TreeNode temp;
            int t=queue.size();
            for(int i=0;i<t;i++)
            {
                temp=queue.poll();
                x.add(temp.val);
                if(temp.left!=null) queue.add(temp.left);
                if(temp.right!=null) queue.add(temp.right);
            }
            sum.add(x);
        }
        return sum;
    }
}

二叉树剪枝
class Solution {
    public TreeNode pruneTree(TreeNode root) {
        if(root==null) return root;
        root.left=pruneTree(root.left);
        root.right=pruneTree(root.right);
        if(root.val == 0 && root.left == null && root.right == null) return null;
        return root;
    }
}

从根节点到叶节点的路径之和
class Solution {
    int k=0;
    int res=0;
    public int sumNumbers(TreeNode root) {
        if(root==null) return 0;
        cal(root,k);
        return res;
    }
    public void cal(TreeNode root,int val)
    {
        if(root==null) return;
        int  k=val*10+root.val;
        if(root.left==null&&root.right==null){
            res=res+k;
            return;   
        }
        cal(root.left,k);
        cal(root.right,k);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值