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.

  这道题难度还好。具体程序(16ms)如下:

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     bool isBalanced(TreeNode* root) {
13         return isBalancedSub(root);
14     }
15
16     bool isBalancedSub(TreeNode *tree)
17     {
18         if(!tree)
19             return true;
20         if(abs(getHeight(tree->left) - getHeight(tree->right)) > 1)
21             return false;
22         return isBalancedSub(tree->left) && isBalancedSub(tree->right);
23     }
24
25     int getHeight(TreeNode *tree)
26     {
27         if(!tree)
28             return 0;
29
30         int m = getHeight(tree->left);
31         int n = getHeight(tree->right);
32         return (m > n) ? m + 1 : n + 1;
33     }
34 };
时间: 2024-08-26 20:51:14

LeetCode之“树”:Balanced Binary Tree的相关文章

LeetCode题解: Balanced Binary Tree

判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/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 subtree

Leetcode solution 110: Balanced Binary Tree

Problem Statement 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. Example 1: Given

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. 题目解答:判断一棵给定的二叉树是不是平衡二叉树.平衡二叉树的条

【一天一道LeetCode】#110. Balanced Binary Tree

一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 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 th

leetcode || 110、Balanced Binary Tree

problem: 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. Hide Tags Tree Depth-first

LeetCode之“树”:Binary Tree Level Order Traversal && Binary Tree Level Order Traversal II

Binary Tree Level Order Traversal 题目链接 题目要求: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,20,#,#,15,7}, 3 / 9 20 / 15 7 return its level orde

LeetCode之“树”:Binary Tree Preorder Traversal && Binary Tree Inorder Traversal && Binary Tree Postorder Traversal

Binary Tree Preorder Traversal 题目链接 题目要求: Given a binary tree, return the preorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 递归解

【LeetCode】110. 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. 提示: 此题要求判断一个给定的二叉树是否是“平衡”的,这里“平

LeetCode题解之Balanced Binary Tree

1.题目描述 2.问题分析 DFS. 3.代码 1 bool isBalanced(TreeNode* root) { 2 if (root == NULL) 3 return true; 4 5 return abs(height(root->left) - height(root->right)) <= 1 && isBalanced(root->left) && isBalanced(root->right); 6 7 } 8 9 int

LeetCode OJ - Balanced Binary Tree

判断树是否是平衡的,这道题中的平衡的概念是指任意节点的两个子树的高度相差不超过1,我用递归的方法把所有的节点的高度都计算了下,并且在计算的过程记录每个节点左右两颗子树的高度差,最后通过遍历这个高度差就可以知道是否是平衡的. 下面是AC代码: 1 /** 2 * Given a binary tree, determine if it is height-balanced. 3 * For this problem, a height-balanced binary tree is defined