天天看點

[leetcode] Balanced Binary Tree

Given a binary tree, determine if it is height-balanced.

For this problem, a height-balanced binary tree is defined as a binary tree

in which the depth of the two subtrees of every node never

differ by more than 1.

給一棵二叉樹,判斷其是否為平衡二叉樹。

使用遞歸,如果一棵樹是平衡二叉樹,那麼他的左子樹和右子樹也應該是平衡二叉樹,而且兩子樹的高度差不超過1。depth為該節點的深度初始化為0。

代碼如下: