Lintcode97 Maximum Depth Of BinaryTree solution 題解

【題目描述】


Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.

給定一個二叉樹,找出其最大深度。

二叉樹的深度爲根節點到最遠葉子節點的距離。

【題目鏈接】

www.lintcode.com/en/problem/maximum-depth-of-binary-tree/

【題目解析】

本題爲查詢樹的深度,我們採用直接遍歷二叉樹,並記錄我們訪問深度的方法即可。

可以考慮用一個全局變量記錄我們的深度,也可以通過DFS函數每一次返回當前子樹的深度。

若採用返回值,則有:當前樹長度= max(左子樹深度, 右子樹深度) + 1

【參考答案】

www.jiuzhang.com/solutions/maximum-depth-of-binary-tree/



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章