【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.

提示:

此题要求判断一个给定的二叉树是否是“平衡”的,这里“平衡”的定义是要求每个节点的左右两颗子树高度之差不能大于1,解题的思想是使用DFS。

代码:

/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 * };
 */
class Solution {
public:
    bool isBalanced(TreeNode* root) {
        return dfs(root) != -1;
    }
    int dfs(TreeNode* root) {
        if (root == NULL) return 0;
        int left = dfs(root->left);
        if (left == -1) return -1;
        int right = dfs(root->right);
        if (right == -1) return -1;

        if (abs(left - right) > 1) return -1;
        return max(left, right) + 1;
    }
};
时间: 2024-11-03 03:45:05

【LeetCode】110. Balanced Binary Tree的相关文章

【LeetCode】110. Balanced Binary Tree-判断是否为平衡二叉树

一.描述: 二.思路 平衡二叉树(Balanced Binary Tree):又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树: 通过每一棵左右子树的深度判断子树是否为平衡二叉树,只有当所有的子树是平衡二叉树时,才能得出整棵树是平衡二叉树: 故函数方法中有两种判断,1是空树?2左右两个子树的高度差的绝对值不超过1? 三.代码: 1 /** 2 * Definition for a binary tr

【一天一道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】998. Maximum Binary Tree II

题目如下: We are given the root node of a maximum tree: a tree where every node has a value greater than any other value in its subtree. Just as in the previous problem, the given tree was constructed from an list A (root = Construct(A)) recursively with

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】226. Invert Binary Tree 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51527554 Subject 出处:https://leetcode.com/problems/invert-binary-tree/ Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Explain 该题目相当简单,就是一个简单的二叉树左右对换结点. Solution solution 1 递归

【LeetCode】106. Construct Binary Tree from Inorder and Postorder Traversal-通过中序和后续遍历还原二叉树

一.描述: 二.思路: 二叉树的中序遍历和前序遍历或和后续遍历能唯一确定一节课二叉树,即2中还原方式都需要中序遍历才能完成: 设二叉树的前序遍历序列为{1, 2, 4, 5, 3, 6},中序遍历序列为{4,2,5,1, 3, 6}:(红色标记表示以还原节点!!!) (1)-前序遍历的第一个节点是二叉树的根节点,{1, 2, 4, 5, 3, 6},对应中序中的位置是{4,2,5,1, 3, 6},所以中序序列中的 '1' 之前的全部元素为左子树元素,'1'之后的为右子树元素: (2)-左子树对

【LeetCode】226. Invert Binary Tree

题目: Invert a binary tree. 4 / 2 7 / \ / 1 3 6 9 to 4 / 7 2 / \ / 9 6 3 1 Trivia:This problem was inspired by this original tweet by Max Howell: Google: 90% of our engineers use the software you wrote (Homebrew), but you can’t invert a binary tree on

LeetCode 110. Balanced Binary Tree 递归求解

题目链接:https://leetcode.com/problems/balanced-binary-tree/ 110. Balanced Binary Tree My Submissions Question Total Accepted: 97926 Total Submissions: 292400 Difficulty: Easy Given a binary tree, determine if it is height-balanced. For this problem, a h

110.Balanced Binary Tree 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. 很早以前做的了  准