var maxDepth = function (root) {

if(!root) return 0;

let stack = [],

cur = root;

```
cur.depth = 1;
while (cur || stack.length) {
if (cur) {
visit(cur);
stack.push(cur);
cur = cur.left;
} else {
cur = stack.pop();
cur = cur.right;
}
}
return maxdepth;
};
function visit(cur) {
const depth = cur.depth;
if (depth > maxdepth) {
maxdepth = depth;
}
if(cur.left){
cur.left.depth = depth + 1;
}
if(cur.right){
cur.right.depth = depth + 1;
}
}
```