二叉樹系列 - 二叉樹的深度,例 [LeetCode]


二叉樹的深度的概念最值得注意的地方,在於 到"葉子"節點的距離。

一般來說,如果直接說“深度”,都是指最大深度,即最遠葉子的距離。

 

這里放兩道例題,最小深度和最大深度。

1. 二叉樹的最小深度

Given a binary tree, find its minimum depth.

The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node.

 1 /**
 2  * Definition for binary tree
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     int minDepth(TreeNode *root) {
13     }
14 };

 

因為深度是必須到葉子節點的距離,因此使用深度遍歷時,不能單純的比較左右子樹的遞歸結果返回較小值,因為對於有單個孩子為空的節點,為空的孩子會返回0,但這個節點並非葉子節點,故返回的結果是錯誤的。

因此,當發現當前處理的節點有單個孩子是空時,返回一個極大值INT_MAX,防止其干擾結果。

 1 class Solution {
 2 public:
 3     int minDepth(TreeNode *root) {
 4         if(!root) return 0;
 5         if(!root -> left && !root -> right) return 1;   //Leaf means should return depth.
 6         int leftDepth = 1 + minDepth(root -> left);
 7         leftDepth = (leftDepth == 1 ? INT_MAX : leftDepth);
 8         int rightDepth = 1 + minDepth(root -> right);
 9         rightDepth = (rightDepth == 1 ? INT_MAX : rightDepth);  //If only one child returns 1, means this is not leaf, it does not return depth.
10         return min(leftDepth, rightDepth);
11     }
12 };

 

當然,這道題也能用層次遍歷來做。

class Solution {
struct LevNode{
    TreeNode* Node;
    int Lev;
};
public:
    int minDepth(TreeNode *root) {
        if(NULL == root) return 0;
        queue<LevNode> q;
        LevNode lnode;
        lnode.Node = root;
        lnode.Lev = 1;
        q.push(lnode);
        while(!q.empty()){
            LevNode curNode = q.front();
            q.pop();
            if(NULL == (curNode.Node) -> left && NULL == (curNode.Node) -> right)
                return (curNode.Lev);
            if(NULL != (curNode.Node) -> left){
                LevNode newNode;
                newNode.Node = (curNode.Node) -> left;
                newNode.Lev = (curNode.Lev + 1);
                q.push(newNode);
            }
            if(NULL != (curNode.Node) -> right){
                LevNode newNode;
                newNode.Node = (curNode.Node) -> right;
                newNode.Lev = (curNode.Lev + 1);
                q.push(newNode);
            }
        }
        return 0;
    }
};

 

對於這道題,LeetCode 兩種解法的時間都是 48ms 

 

2. 二叉樹的最大深度

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.

最大深度也是到葉子節點的長度,但是因為是求最大深度,單個孩子為空的非葉子節點不會干擾到結果,因此用最簡潔的處理方式就可以搞定。

/**
 * Definition for binary tree
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    int maxDepth(TreeNode *root) {
        if(!root) return 0;
        int leftDepth = maxDepth(root -> left) + 1;
        int rightDepth = maxDepth(root -> right) + 1;
        return max(leftDepth, rightDepth);
    }
};

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM