二叉树的层序遍历

leetcode 102 二叉树的层序遍历

给定一个二叉树,返回其按层次遍历的节点值。 (即逐层地,从左到右访问所有节点)。

例如:

给定二叉树: [3,9,20,null,null,15,7],

    3
   / \
  9  20
    /  \
   15   7
返回其层次遍历结果:

[
  [3],
  [9,20],
  [15,7]
]
import sun.reflect.generics.tree.Tree;

import java.util.*;

public class Main {
    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
    }

    public class Item{
        TreeNode node;
        int rank;
        public Item(TreeNode node,int rank){
            this.node = node;
            this.rank = rank;
        }
    }

    public static void main(String[] args) {

    }

    public List<List<Integer>> levelOrder(TreeNode root) {
        Queue<Item> queue = new LinkedList<Item>();
        List<List<Integer>> res = new LinkedList<List<Integer>>();
        if (root == null){
            return res;
        }
        queue.offer(new Item(root,0));
        while (!queue.isEmpty()){
             Item item = queue.poll();
             if (res.size() == item.rank){
                 List<Integer> rankList = new LinkedList<Integer>();
                 rankList.add(item.node.val);
                 res.add(rankList);
             }else {
                 res.get(item.rank).add(item.node.val);
             }
             if (item.node.left != null){
                 queue.offer(new Item(item.node.left,item.rank + 1));
             }
             if (item.node.right != null){
                 queue.offer(new Item(item.node.right, item.rank + 1));
             }
        }
        return res;
    }
}

leetCode 279完全平方数

给定正整数 n,找到若干个完全平方数(比如 1, 4, 9, 16, …)使得它们的和等于 n。你需要让组成和的完全平方数的个数最少。
示例 1:

输入: n = 12
输出: 3 
解释: 12 = 4 + 4 + 4.
示例 2:

示例 2:
其实这个问题可以看成是一个图的问题,使用图的层次序列遍历解决在这里插入图片描述

输入: n = 13
输出: 2
解释: 13 = 4 + 9.

code

import sun.reflect.generics.tree.Tree;

import javax.sound.midi.Soundbank;
import java.util.*;

public class Main {
    public static void main(String[] args) {
        System.out.println(numSquares(13));
    }

    static class Item{
        int val;
        int rank;
        Item(int val, int rank){
            this.val = val;
            this.rank = rank;
        }
    }

    public static int numSquares(int n) {
        Queue<Item> queue = new LinkedList<Item>();
        boolean[] isIn = new boolean[n];
        queue.offer(new Item(n,0));
        while (!queue.isEmpty()){
            Item item = queue.poll();
            for (int i=1; i<=(int)Math.sqrt(item.val); i++){
                int val = item.val-(int)Math.pow(i,2);
                if (val == 0){
                    return item.rank + 1;
                }
                if (isIn[val] == false){
                    queue.offer(new Item(val,item.rank + 1));
                    isIn[val] = true;
                }
            }
        }
        return 0;
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值