二叉樹的深度(劍指offer)
阿新 • • 發佈:2017-06-09
item val 二叉 node oot 劍指offer style spa pub
題目描述
輸入一棵二叉樹,求該樹的深度。從根結點到葉結點依次經過的結點(含根、葉結點)形成樹的一條路徑,最長路徑的長度為樹的深度。1 /* 2 struct TreeNode { 3 int val; 4 struct TreeNode *left; 5 struct TreeNode *right; 6 TreeNode(int x) : 7 val(x), left(NULL), right(NULL) { 8 } 9 };*/ 10 class Solution { 11 public: 12 intTreeDepth(TreeNode* pRoot) 13 { 14 if(pRoot == NULL){ 15 return 0; 16 } 17 int left = 0; 18 int right = 0; 19 left = TreeDepth(pRoot->left); 20 right = TreeDepth(pRoot->right); 21 return (left > right ? left : right) + 1; 22} 23 };
二叉樹的深度(劍指offer)