AcWing 71. 二叉树的深度 java非递归
原题链接
简单
作者:
joyonline
,
2019-12-21 21:49:07
,
所有人可见
,
阅读 897
class Solution {
public int treeDepth(TreeNode root) {
if(root == null){
return 0;
}
LinkedList<TreeNode> list = new LinkedList();
list.add(root);
int count = 0;
while(root != null && !list.isEmpty()){
int len = list.size();
count++;
while(len > 0){
TreeNode tmp = list.pop();
if(tmp.left != null) {
list.add(tmp.left);
root = tmp.left;
}
if(tmp.right != null){
list.add(tmp.right);
root = tmp.right;
}
len--;
}
}
return count;
}
}