Suppose I build a toy tree model with RPART, how can I get the depth of the tree?
fit=rpart(factor(am)~.,mtcars,control=rpart.control(cp=0,minsplit = 1))
rpart has a unexported function
tree.depth that gives the depth of each node in the vector of node numbers passed to it. Using data from the question:
nodes <- as.numeric(rownames(fit$frame)) max(rpart:::tree.depth(nodes)) ##  2