实现一个函数检查一棵树是否平衡。对于这个问题而言,平衡指的是这棵树任意两个叶子结点到根结点的距离之差不大于1
示例1
输入
{1,2,3,#,#,4,#,#,5}
输出
false
说明
样例树形状
1
/ \
2 3
/
4
\
5
加载中...
import java.util.*; /* * public class TreeNode { * int val = 0; * TreeNode left = null; * TreeNode right = null; * } */ public class Solution { /** * * @param root TreeNode类 树的根节点 * @return bool布尔型 */ public boolean isBalanced (TreeNode root) { // write code here } }
/** * struct TreeNode { * int val; * struct TreeNode *left; * struct TreeNode *right; * }; */ class Solution { public: /** * * @param root TreeNode类 树的根节点 * @return bool布尔型 */ bool isBalanced(TreeNode* root) { // write code here } };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 树的根节点 # @return bool布尔型 # class Solution: def isBalanced(self , root ): # write code here
/* * function TreeNode(x) { * this.val = x; * this.left = null; * this.right = null; * } */ /** * * @param root TreeNode类 树的根节点 * @return bool布尔型 */ function isBalanced( root ) { // write code here } module.exports = { isBalanced : isBalanced };
# class TreeNode: # def __init__(self, x): # self.val = x # self.left = None # self.right = None # # # @param root TreeNode类 树的根节点 # @return bool布尔型 # class Solution: def isBalanced(self , root ): # write code here
{1,2,3,#,#,4,#,#,5}
false