Code Monkey home page Code Monkey logo

Comments (2)

PisecesPeng avatar PisecesPeng commented on September 25, 2024

解题思路

  • 将临时列表中存入根node, 开始遍历,
  • 将临时列表中所有node的值存入结果列表中,
  • 清空临时列表, 将下次循环需要遍历的node的子节点存入临时列表中.

代码

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;
    }

}
public static List<List<Integer>> func(TreeNode root) {
   List<TreeNode> tmpList = new ArrayList<>();
   if (null != root) tmpList.add(root);

   List<List<Integer>> x = new ArrayList<>();  // 存放结果
   while (tmpList.size() > 0) {
       // 将本层的节点复制到新list中, 并将老list清空
       List<TreeNode> tmp = new ArrayList();
       tmp.addAll(tmpList);
       tmpList.clear();
       List<Integer> y = new ArrayList<>();
       for (TreeNode node : tmp) {
           // 将本层的值统计
           y.add(node.val);
           // 将本层的节点按顺序添加, 为下一次循环
           if (null != node.left)
               tmpList.add(node.left);
           if (null != node.right)
               tmpList.add(node.right);
       }
       x.add(y);
   }
   return x;
}

from pisecespeng.record.me.

PisecesPeng avatar PisecesPeng commented on September 25, 2024

LeetCode题解

解题思路

  • 将根node存入队列, 开始遍历,
  • 将node推出队列, node值存入结果列表,
  • 将node左右节点存入队列, 作为下次循环数据.

代码

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;
    }

}
public static List<List<Integer>> func(TreeNode root) {
    List<List<Integer>> ret = new ArrayList<List<Integer>>();
    if (root == null) {
        return ret;
    }

    Queue<TreeNode> queue = new LinkedList<TreeNode>();
    queue.offer(root);
    while (!queue.isEmpty()) {
        List<Integer> level = new ArrayList<Integer>();
        int currentLevelSize = queue.size();
        for (int i = 1; i <= currentLevelSize; ++i) {
            TreeNode node = queue.poll();
            level.add(node.val);
            if (node.left != null) {
                queue.offer(node.left);
            }
            if (node.right != null) {
                queue.offer(node.right);
            }
        }
        ret.add(level);
    }
    
    return ret;
}

from pisecespeng.record.me.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.