110. Balanced Binary Tree (Tree; DFS)

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.

struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};
class Solution {
public:
    bool isBalanced(TreeNode *root) {
        if(!root) return true;

        flag = true;
        dfs(root,1);
        return flag;

    }
    int dfs(TreeNode* node, int depth){
        if(!flag)
        {
            return 0;
        }
        if(!node->left && !node->right)
        {
            return depth;
        }

        int left=depth;
        int right=depth;
        if(node->left)
        {
            left = dfs(node->left,depth+1);
        }

        if(node->right)
        {
            right = dfs(node->right,depth+1);
        }

        if(abs(left-right)> 1)
        {
            flag = false;
        }
        return max(left,right);
    }
private:
    bool flag;
};
时间: 2024-10-16 03:53:59

110. Balanced Binary Tree (Tree; DFS)的相关文章

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

leetCode 110. Balanced Binary Tree 平衡二叉树

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. 题目大意: 判断一

108. Convert Sorted Array to balanced Binary Search Tree

108. Convert Sorted Array to balanced Binary Search Tree The tricky part is the base case . Write induction part first and then test arrays of different size, 0, 1,2, 3 And finalize the base case /** * Definition for a binary tree node. * public clas

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】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

题目: 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 (平衡二叉树) 解题思路和方法

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. 思路:判断是否是平衡二叉树,dfs即可,递归判断每个子树是否平衡二叉树

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 binarytree in which the depth of the two subtrees of every nodenever differ by more than 1. HideTags Tree Depth-first Search #pra