public int maxDepth2(TreeNode root) {
		// Note: The Solution object is instantiated only once and is reused by
		// each test case.
		Stack<TreeNode> stack = new Stack<TreeNode>();
		HashSet<TreeNode> visited = new HashSet<TreeNode>();
		TreeNode node = null;
		if (root == null)
			return 0;
		stack.push(root);
		int depth = 0;
		int tmp = 0;
		while (!stack.isEmpty()) {
			node = stack.peek();
			if (visited.contains(node)) {
				--tmp;
				stack.pop();
				continue;
			}
			visited.add(node);
			++tmp;
			if (node.left == null && node.right == null) {
				depth = Math.max(depth, tmp--);
				stack.pop();
			}
			if (node.right != null)
				stack.push(node.right);
			if (node.left != null)
				stack.push(node.left);
		}
		return depth;
	}