题目: 

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.

 

思路1: We can solve this easily using recursion. Why? Because each of the leftChild and rightChild of a node is a sub-tree itself. We first compute the max height of left sub-tree, then compute the max height of right sub-tree. Therefore, the max height of the current node is the greater of the two max heights + 1. For the base case, the current node is NULL, we return zero. NULL signifies there is no tree, therefore its max height is zero.

代码:(DFS--迭代):

class Solution {
public:
    int maxDepth(TreeNode *root) {
    
        if(root == NULL) return 0;
        return 1 + max(maxDepth(root->left), maxDepth(root->right));
    }
};

 或者用下面一种比较笨的方法来在遍历的过程中更新最大class Solution {

public:
    
    void helper(TreeNode *root, int count, int &maxDep){
        
        if (root == NULL) maxDep = max(count, maxDep);
        
        else{
            
            count+=1;
            helper(root->left, count, maxDep);
            helper(root->right, count, maxDep);
        } 
    }
    
    int maxDepth(TreeNode *root) {
        
        if (root == NULL) return 0;
        
        int maxDep = 0;
        int count = 0;
        helper(root, count, maxDep);
        return maxDep;
    }

 

思路2: 对于iteratively的解法,我们有三种解法:pre-order, in-order, and post-order all as DFS. 前序遍历和中序遍历更容易实现,但是有可能会在一个node被pop出stack的时候在这个node上面几层跳出循环。但是后序遍历可以保证在一个node被pop出去的时候返回恰好这个node上面一层。所以我们可以用后序遍历来做这个题。We keep track of the current depth and update the maximum depth as we traverse the tree.

代码:

class Solution {
public:
    int maxDepth(TreeNode *root) {
    
        if(root == NULL) return 0;
        stack<TreeNode*> s;
        
        s.push(root);
        int maxDep = 0;
        TreeNode *prev = NULL;
        while(!s.empty()){
            
            TreeNode* curr = s.top();
            if (!prev || prev->left == curr || prev->right == curr){
                
                if (curr->left) s.push(curr->left);
                else if (curr->right) s.push(curr->right);
            }
            
            else if (curr->left == prev){
                
                if (curr->right) s.push(curr->right);
            }
            
            else s.pop();
            
            prev = curr;
            
            if (s.size()>maxDep) maxDep = s.size();
        }  
         
    return maxDep;
    }
};

 

思路3:第三种解法是BFS, BFS的解法可以很直观。每次一层结束的时候就把层数加一。 重点是queue和每一层节点数的记录。

 

代码:

class Solution {
public:
    int maxDepth(TreeNode *root) {
    // Start typing your C/C++ solution below
    // DO NOT write int main() function
    if( root == NULL) return 0;

    queue<TreeNode*> q;
    q.push(root);
    
    int count = q.size();
    int maxDep = 0;
    
    while(!q.empty())
    {
        count--;
        if( count == 0 )
            maxDep++;
        TreeNode* p = q.front();
        q.pop();
        if( p->left)  q.push(p->left);
        
        if( p->right )  q.push(p->right);
        
        if( count == 0 ) count = q.size();
    }
    
    return maxDep;
}
};

  

 

posted on 2013-04-30 11:59  tanghulu321  阅读(108)  评论(0编辑  收藏  举报