本题要求判断给定的二叉树是否是平衡二叉树 平衡二叉树的性质为: 要么是一棵空树,要么任何一个节点的左右子树高度差的绝对值不超过 1。 一颗树的高度指的是树的根节点到所有节点的距离中的最大值。
示例1
输入
{1,#,2,#,3}
输出
false
示例2
输入
{2,1,3}
输出
true
加载中...
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
package main import . "nc_tools" /* * type TreeNode struct { * Val int * Left *TreeNode * Right *TreeNode * } */ /** * * @param root TreeNode类 * @return bool布尔型 */ func isBalanced( root *TreeNode ) bool { // write code here }
{1,#,2,#,3}
false
{2,1,3}
true