8 ms C language solution


  • 0

    The code is composed by two parts. The first one used to calculate the max depth of a node.

    int depth(struct TreeNode* rt)//求出当前节点最深深度
    {
    	if (!rt)
    		return 0;
    	int v1 = depth(rt->left);
    	int v2 = depth(rt->right);
    	return 1 + (v1 > v2 ? v1 : v2);
    }
    

    Second parts used to make the "current node" traverse all nodes.

    bool isBalanced(struct TreeNode* rt) 
    {
    	if (!rt)
    		return 1;
    	int v1 = depth(rt->left);
    	int v2 = depth(rt->right);
    	if (abs(v1 - v2) > 1)//当前节点左右的最深深度比较
    		return 0;
    	return isBalanced(rt->left)*isBalanced(rt->right);//将当前节点细化
    	//return 1;
    }

  • 0

    The test case #121 is quite interesting.


  • 0
    A
    This post is deleted!

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.