zoukankan      html  css  js  c++  java
  • Maximum Depth of Binary Tree

    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.

    点击打开原题链接

    这个题剑指offer里也有,简单的递归就可以,代码非常清晰:

    class Solution 
    	{
    	public:
    		int maxDepth(TreeNode *root) 
    		{
    			if (root == NULL)
    			{
    				return 0;
    			}
    			else
    			{
    				int a = maxDepth(root->left) + 1;
    				int b = maxDepth(root->right) + 1;
    				if (a > b)
    				{
    					return a;
    				} 
    				else
    				{
    					return b;
    				}
    			}
    		}
    	private:
    		//static int depth;
    	};


  • 相关阅读:
    #pragma
    STL~heap
    codeforces682A
    POJ1753(位操作和枚举)
    位运算
    getchar()(转)
    UVA2636
    UVA2639
    UVA2322
    UVa2521
  • 原文地址:https://www.cnblogs.com/yjbjingcha/p/6814824.html
Copyright © 2011-2022 走看看