2021.9.3刷题

求根节点到叶节点数字之和
class Solution {
    static int sum;
    public int sumNumbers(TreeNode root) {
        sum=0;
        Sum(0,root);
        return sum;
    }
    public static void Sum(int val,TreeNode root)
    {
        if(root==null) return;
        int k=(val*10+root.val);
        if(root.left==null&&root.right==null)
        {
            sum=sum+k;
        }
        Sum(k,root.left);
        Sum(k,root.right);
    }
}

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

二叉树的层序遍历 II
class Solution {
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> sum=new LinkedList<>();
        if(root==null) return sum;
        Queue<TreeNode> x=new LinkedList<>();
        x.add(root);
        while(!x.isEmpty())
        {
            TreeNode temp;
            List<Integer> y=new ArrayList<>();
            int count = x.size();
            for (int i=0;i<count;i++) {
                TreeNode node = x.poll();
                y.add(node.val);
                if (node.left != null)
                    x.add(node.left);
                if (node.right != null)
                    x.add(node.right);
            }
            sum.add(y);
        }
        Collections.reverse(sum);
        return sum;
    }
}

二叉树的最大深度
class Solution {
    public int maxDepth(TreeNode root) {
        if(root==null) return 0;
        return Math.max((maxDepth(root.left)+1),maxDepth(root.right)+1);

二叉树的层序遍历
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
    List<List<Integer>> sum=new LinkedList<>();
    if(root==null) return sum;
    Queue<TreeNode> queue=new LinkedList<>();
    queue.add(root);
    while(!queue.isEmpty())
    {
        int count=queue.size();
        List<Integer> x=new ArrayList<>();
        TreeNode temp;
        for(int i=0;i<count;i++)
        {
            temp=queue.peek();
            queue.remove();
            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 boolean isSymmetric(TreeNode root) {
        if(root==null) return true;
        return cal(root.left,root.right);
    }
    public boolean cal(TreeNode node1,TreeNode node2)
    {
        if(node1==null&&node2==null) return true;
        if(node1==null||node2==null||node1.val!=node2.val) return false;
        return cal(node1.left,node2.right)&&cal(node1.right,node2.left);
    }
}
左叶子之和
class Solution {
    public int sumOfLeftLeaves(TreeNode root) {
        int x=0;
        if(root==null) return 0;
        if(root.left!=null)
        {
            if(root.left.left==null&&root.left.right==null)
            {
                x=x+root.left.val;
            }
        }
        x=x+sumOfLeftLeaves(root.left)+sumOfLeftLeaves(root.right);
        return x;
    }
}

二叉搜索树中的众数
class Solution {
    int preVal = 0, curTimes = 0, maxTimes = 0;
    ArrayList<Integer> list = new ArrayList<Integer>();
    public int[] findMode(TreeNode root) {
        ArrayList<Integer> sum = new ArrayList<Integer>();
        sum=cal(root);
        int size = sum.size();
	      int[] ans = new int[size];
	   	  for(int i = 0; i < size; i++){
	         ans[i] = sum.get(i);
	    	}
        return ans;
    }
    public ArrayList<Integer> cal(TreeNode root)
    {
        if(root==null) return list;
        cal(root.left);
        if(preVal == root.val){
		    	curTimes++;
	    	}else{
		    	preVal = root.val;
		    	curTimes = 1;
	    	}
	    	//判断当前数量与最大数量的关系, 更新 list 和 maxTimes
	    	if(curTimes == maxTimes){
		   		list.add(root.val);
	    	}
        else if(curTimes > maxTimes){
		    	list.clear();
		    	list.add(root.val);
		    	maxTimes = curTimes;
	    	}
        cal(root.right);
        return list;
    }
}

二叉搜索树中的最小绝对值差
class Solution {
    private int result = Integer.MAX_VALUE;
    private TreeNode preNode = null;
    public int getMinimumDifference(TreeNode root) {
        return getMin(root);
    }
    public int getMin(TreeNode root)
    {
        if(root == null){
            return result;
        }
        getMin(root.left);
        if(preNode != null)
        {
            result = Math.min(Math.abs(root.val - preNode.val), result);
        }
        preNode = root;
        getMin(root.right);
        return result;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值