My Java 2 solutions 1ms(dfs) & 2ms(bfs)


  • 7
    ​/**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            if (root == null)
                return res;
            Queue<TreeNode> q = new LinkedList<TreeNode>();
            q.offer(root);
            while (!q.isEmpty()) {
                List<Integer> tmp = new ArrayList<Integer>();
                int size = q.size();
                for (int i = 0; i < size; ++i) {
                    TreeNode t = q.poll();
                    if (t.left != null)
                        q.offer(t.left);
                    if (t.right != null)
                        q.offer(t.right);
                    tmp.add(t.val);
                }
                res.add(tmp);
            }
            return res;
        }
    }
    
    
    /**
     * Definition for a binary tree node.
     * public class TreeNode {
     *     int val;
     *     TreeNode left;
     *     TreeNode right;
     *     TreeNode(int x) { val = x; }
     * }
     */
    public class Solution {
        public List<List<Integer>> levelOrder(TreeNode root) {
            List<List<Integer>> res = new ArrayList<List<Integer>>();
            dfs(res,root,0);
            return res;
        }
        public void dfs(List<List<Integer>> list,TreeNode node,int deep){
            if(node==null)return;
            if(list.size()==deep)
                list.add(new ArrayList<Integer>());
            list.get(deep).add(node.val);
            dfs(list, node.left, deep+1);
            dfs(list, node.right, deep+1);    
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.