美文网首页TREE
107. Binary Tree Level Order Tra

107. Binary Tree Level Order Tra

作者: DrunkPian0 | 来源:发表于2017-04-03 21:12 被阅读21次

    有点感动,这题在AS里写好之后复制过去一次AC了。
    这种BFS的套路跟这题的第一个版本一样,用queue,维护curNum和nextNum;我跟code ganker学会了这个套路,按照覃超的标准来说这代码是有点长的,但是现在已经形成记忆了,就不去修改了。思路还蛮清晰的。

        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> res = new ArrayList<>();
            if (root == null) return res;
    
            LinkedList<TreeNode> queue = new LinkedList<>();
            List<Integer> cell = new ArrayList<>();
            queue.add(root);
            int curNum = 1;
            int nextNum = 0;
            while (!queue.isEmpty()) {
                TreeNode node = queue.poll();
                cell.add(node.val);
                curNum--;
    
                if (node.left != null) {
                    queue.offer(node.left);
                    nextNum++;
                }
    
                if (node.right != null) {
                    queue.offer(node.right);
                    nextNum++;
                }
    
                if (curNum == 0) {
                    res.add(0, cell);
                    curNum = nextNum;
                    nextNum = 0;
                    cell = new ArrayList<>();
                }
            }
            return res;
        }
    


    贴一下leetcode里面的人的简洁写法:https://discuss.leetcode.com/topic/7651/my-dfs-and-bfs-java-solution/2

    public class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            Queue<TreeNode> queue = new LinkedList<TreeNode>();
            List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
            
            if(root == null) return wrapList;
            
            queue.offer(root);
            while(!queue.isEmpty()){
                int levelNum = queue.size();
                List<Integer> subList = new LinkedList<Integer>();
                for(int i=0; i<levelNum; i++) {
                    if(queue.peek().left != null) queue.offer(queue.peek().left);
                    if(queue.peek().right != null) queue.offer(queue.peek().right);
                    subList.add(queue.poll().val);
                }
                wrapList.add(0, subList);
            }
            return wrapList;
        }
    }
    

    递归(我没看):

    public class Solution {
            public List<List<Integer>> levelOrderBottom(TreeNode root) {
                List<List<Integer>> wrapList = new LinkedList<List<Integer>>();
                levelMaker(wrapList, root, 0);
                return wrapList;
            }
            
            public void levelMaker(List<List<Integer>> list, TreeNode root, int level) {
                if(root == null) return;
                if(level >= list.size()) {
                    list.add(0, new LinkedList<Integer>());
                }
                levelMaker(list, root.left, level+1);
                levelMaker(list, root.right, level+1);
                list.get(list.size()-level-1).add(root.val);
            }
        }
    

    相关文章

      网友评论

        本文标题:107. Binary Tree Level Order Tra

        本文链接:https://www.haomeiwen.com/subject/yzboottx.html