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 ofeverynode never differ by more than 1. Example 1: Given the following tree[3,9,20,null,null,15,7]: 3 /...
}privateintgetDepth(TreeNode tree,intcurrentDepth){if(tree ==null){returncurrentDepth; }returnMath.max(getDepth(tree.left, currentDepth+1), getDepth(tree.right, currentDepth+1)); } } 写了一个getDepth()函数,访问每个节点都要调用一次这个函数。这个Solution也通过了leetcode的验证程序,但是后来想...
https://leetcode.com/submissions/detail/40087813/ Total Accepted: 72203 Total Submissions: 225370 Difficulty: Easy 题目描述 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...
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1. 思路:当树的左子树的高度与右子树的高度相差小于一,且左右子树各自都为平衡二叉树时,树为平衡二叉树。 代码: boolSolution::isBalanced(T...
My code: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicbooleanisBalanced(TreeNoderoot){if(root==null)returntrue;returnhelper(root)==-1?false:tru...
Given a binary tree, determine if it is height-balanced. a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 判断一个二叉树是否为平衡二叉树 关键: 1 平衡二叉树的每一个节点的子数深度相差小于1 ...
思路:https://leetcode.com/problems/balanced-binary-tree/discuss/35691/The-bottom-up-O(N)-solution-would-be-better.左右子树的高度差不能超过1。 工程代码下载 /** * Definition for a binary tree node. * struct TreeNode { * int val;
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 ofeverynode never differ by more than 1. Example 1:
For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees ofeverynode never differ by more than 1 参数传递法: bool isBalanced(TreeNode *root) { if(!root) return true;
Can you solve this real interview question? Balanced Binary Tree - Given a binary tree, determine if it is height-balanced. Example 1: [https://assets.leetcode.com/uploads/2020/10/06/balance_1.jpg] Input: root = [3,9,20,null,null,15,7] Output: tru