/**
public class TreeNode {
int val = 0;
TreeNode left = null;
TreeNode right = null;
public TreeNode(int val) {
this.val = val;
}
}
*/
import java.util.*;
public class Solution {
/**********************************************************************************/
// 非递归方式
/*
public int TreeDepth(TreeNode root) {
if (null == root) {
return 0;
}
Queue<TreeNode> queue = new LinkedList<>();
TreeNode node = root;
HashMap<TreeNode, Integer> hashMap = new HashMap<>();
int res = 0;
queue.add(node);
hashMap.put(node, 1);
while (!queue.isEmpty()) {
node = queue.poll();
int currentLevel = hashMap.get(node);
res = Math.max(res, currentLevel);
if (null != node.left) {
queue.add(node.left);
hashMap.put(node.left, currentLevel + 1);
}
if (null != node.right) {
queue.add(node.right);
hashMap.put(node.right, currentLevel + 1);
}
}
return res;
}
*/
/**********************************************************************************/
// 递归方式
public int TreeDepth(TreeNode root) {
if (null == root) {
return 0;
}
if (null == root.left && null == root.right) {
return 1;
}
return Math.max(TreeDepth(root.left), TreeDepth(root.right)) + 1;
}
}