标签:subject tps pid rac tree coder dep 遍历 pre
/** public class TreeNode { int val = 0; TreeNode left = null; TreeNode right = null; public TreeNode(int val) { this.val = val; } } */ public class Solution { int maxDepth = 0; public int TreeDepth(TreeNode root) { if(null == root){ return 0; } getDepth(root,1); return maxDepth; } public void getDepth(TreeNode cur,int depth){ //叶节点判断深度 if(cur.left == null && cur.right == null){ if(depth > maxDepth){ maxDepth = depth; } return; } //左子树遍历 if(cur.left != null){ getDepth(cur.left,depth+1); } //右子树遍历 if(cur.right != null){ getDepth(cur.right,depth+1); } } }
标签:subject tps pid rac tree coder dep 遍历 pre
原文地址:https://www.cnblogs.com/MoonBeautiful/p/13070117.html