My accepted solution in Java without recursion


  • 4
    S
      public int maxDepth(TreeNode root) {
        int depth = 0;
        if(root==null) return depth;
        LinkedList<TreeNode> list = new LinkedList<TreeNode>();
        list.offer(root); TreeNode levelLast = root;
        while(list.size()>0){
            TreeNode curr = list.poll();
            if(curr.left!=null) list.offer(curr.left);
            if(curr.right!=null) list.offer(curr.right);
            if(curr==levelLast) {
                levelLast = list.peekLast();
                depth++;
            }
        }
        return depth;
    }

  • 0
    L
    This post is deleted!

Log in to reply
 

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