面试39---二叉树的深度

题目链接

求解二叉树的深度,延伸可见leetcode110题。

法一:dfs。

复制代码
1     private int TreeDepth(TreeNode root) {
2         if(root == null) {
3             return 0;
4         }
5         int l = TreeDepth(root.left);
6         int r = TreeDepth(root.right);
7         return (l > r) ? (l + 1) : (r + 1);
8     }
View Code
复制代码

 

posted on   二十年后20  阅读(73)  评论(0编辑  收藏  举报

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示