/**
非递归方式,没啥好说的,用了两个list来实现。看到有别的同学用的一个list加一个int,空间上更优一些。


public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
import java.util.ArrayList;

public class Solution {
    public int TreeDepth(TreeNode root) {
        if (root == null) {
            return 0;
        }
        if(root.left == null && root.right == null){
            return 1;
        }

        int count = 0;
        ArrayList<TreeNode> list = new ArrayList<TreeNode>();
        list.add(root);

        while (list.size() != 0) {
            ArrayList<TreeNode> nextDeepNode = new ArrayList<TreeNode>();
            for (TreeNode node : list) {
                if(node.left != null){
                   nextDeepNode.add(node.left);
                 }
                if(node.right != null){
                    nextDeepNode.add(node.right);
                }
            }
            count++;
            list = nextDeepNode;
        }

        return count;
    }
}