GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/code/lc102.java at master · mJackie/leetcode Expand file tree/ Copy pathlc102.javaMore file actions 44 lines (42 loc) · 1.18 KB / Copy pathlc102.javaFile metadata and controls44 lines (42 loc) · 1.18 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 package code; /* * 102. Binary Tree Level Order Traversal * 题意:二叉树层次遍历 * 难度:Medium * 分类:Tree, Breadth-first Search * 思路:用一个队列 * Tips:如何判断遍历完一层了?pop的时候先看下size */ import java.util.*; public class lc102 { public class TreeNode { int val; TreeNode left; TreeNode right; TreeNode(int x) { val = x; } } public List<List<Integer>> levelOrder(TreeNode root) { Queue<TreeNode> qu = new LinkedList<>(); List<List<Integer>> res = new ArrayList<>(); if(root==null) return res; qu.add(root); while(!qu.isEmpty()){ //两个while int size = qu.size(); List<Integer> temp = new ArrayList<>(); while(size>0){ TreeNode tn = qu.remove(); if(tn!=null) { if(tn.left!=null) qu.add(tn.left); if(tn.right!=null) qu.add(tn.right); } temp.add(tn.val); size--; } res.add(temp); } return res; } } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |