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.

题目解答:判断一棵给定的二叉树是不是平衡二叉树。平衡二叉树的条件有:

    它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。

对于树的处理,一般都使用递归的方式。

代码:

/**
 * 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) {
        if(root == NULL)
            return true;
        if((root -> left == NULL ) && (root -> right == NULL))
            return true;
        int leftDepth = 0;
        int rightDepth = 0;
        return isSubBalance(root -> left,leftDepth) && isSubBalance(root -> right , rightDepth) && (abs(leftDepth - rightDepth) <= 1) ;
    }
   
    bool isSubBalance(TreeNode *root,int &depth)
    {
        if(root == NULL)
        {
            depth = 0;
            return true;
        }
        if((root -> left == NULL ) && (root -> right == NULL))
        {
            depth = 1;
            return true;
        }
        else
        {
            depth = 1;
            int leftDepth = 0;
            int rightDepth = 0;
            return isSubBalance(root -> left,leftDepth) && isSubBalance(root -> right , rightDepth) && (abs(leftDepth - rightDepth) <= 1) && (depth += max(leftDepth,rightDepth) ) ;
        }
    }
   
};

时间: 2024-08-05 15:19:45

Leetcode题目:Balanced Binary Tree的相关文章

LeetCode题目:Balanced Binary Tree

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. tags:深度优先遍历 c

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

leetcode -day18 Balanced Binary Tree

测试源码: //测试派生类的构造函数的调用顺序何时调用 //Fedora20 gcc version=4.8.2 #include <iostream> using namespace std; class base { public: base() { cout<<"base created!"<<endl; } ~base() { cout<<"base destroyed!"<<endl; } };

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

[LeetCode][Java] 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][JavaScript]Balanced Binary Tree

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. https://leetco

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

【leetcode】Balanced Binary Tree(middle)

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 解题报告

[题目] 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题目:Binary Tree Paths

题目: Given a binary tree, return all root-to-leaf paths. For example, given the following binary tree: 1 / 2 3 5 All root-to-leaf paths are: ["1->2->5", "1->3"] 题目解答:使用递归的方式来处理这道题目,每到叶子节点,就进行一次输出. 代码如下: /** * Definition for a b