Accepted C Solution using post-order traversal,one pass

  • 0

    it's short and simple,the tree traversal is classic when solving problems like this.

    idea behind the scene is to check left and right branches,then return the height of this node.

    bool isBalancedRetHeight(struct TreeNode *root,int *height) {
    		return true;
    	int l_d;
    	if(!isBalancedRetHeight(root->left,&l_d)) return false;
    	int r_d;
    	if(!isBalancedRetHeight(root->right,&r_d)) return false;
    	if(abs(l_d-r_d)>1) return false;
    	*height= (l_d>r_d?l_d:r_d)+1;
    	return true;
    bool isBalanced(struct TreeNode* root) {
    	int height=0;
    	return isBalancedRetHeight(root,&height);

Log in to reply

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