美文网首页Leetcode/Java学习笔记
107. Binary Tree Level Order Tra

107. Binary Tree Level Order Tra

作者: 萧瑟空间 | 来源:发表于2018-09-02 07:57 被阅读0次

    对于这道BFS题目的两种不同解法(two queues or one queue)

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            if(root == null){
                return new ArrayList<>();
            }
            //注意初始化Queue的方法,一般实例为LinkedList
            Queue<TreeNode> nodeQueue = new LinkedList<TreeNode>();
            nodeQueue.add(root);
            List<Integer> tempList = new ArrayList<Integer>();
            List<List<Integer>> ans = new ArrayList<>();
            while(nodeQueue.size() > 0){
            //用size来track换行的时机
                int length = nodeQueue.size();
                tempList.clear();
                while(length > 0){
                    TreeNode curHead = nodeQueue.poll();
                    if(curHead.left != null){
                        nodeQueue.add(curHead.left);
                    }
                    if(curHead.right != null){
                        nodeQueue.add(curHead.right);
                    }
                    tempList.add(curHead.val);
                    length--;
                }
            //必须新建一个与tempList相同的ArrayList添加入ans,否则后续会改变其内容
                ans.add(new ArrayList<Integer>(tempList));
            }
            //翻转ArrayList
            Collections.reverse(ans);
            return ans;
        }
    }
    

    用两个List的好处就是可以不需要track什么时候换行,递归一次就是一行结束

    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    class Solution {
        public List<List<Integer>> levelOrderBottom(TreeNode root) {
            List<List<Integer>> ans = new ArrayList<>();
            List<TreeNode> startList = new ArrayList<>();
            if(root == null){
                return new ArrayList<>();
            }
            startList.add(root);
            helper(startList, ans);
            Collections.reverse(ans);
            return ans;
        }
        
        public void helper(List<TreeNode> nodeList, List<List<Integer>> result){
            List<Integer> tempList = new ArrayList<Integer>();
            List<TreeNode> childList = new ArrayList<>();
            for(TreeNode ele: nodeList){
                tempList.add(ele.val);
                if(ele.left != null){
                    childList.add(ele.left);
                }
                if(ele.right != null){
                    childList.add(ele.right);
                }
            }
            result.add(tempList);
            //递归一次之后,如果子节点List不为空说明还有更深的节点,那么再递归一次
            if(childList.size() >= 1){
                helper(childList, result);
            }
            //反之,如若子节点List为空,则说明已经递归到了最深的一行,递归结束
        }
    }
    

    相关文章

      网友评论

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

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