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

#pragma once
#include<iostream>
using namespace std;

struct TreeNode {
	int val;
	TreeNode *left;
	TreeNode *right;
	TreeNode(int x) : val(x), left(NULL), right(NULL) {}
};

//取最大值
int max(int a, int b)
{
	if (a > b)
		return a;
	return b;
}

//求深度
int findDepth(TreeNode* p)
{
	if (!p)
		return 0;
	return max(findDepth(p->left), findDepth(p->right)) + 1;
}

//平衡条件:左平衡+右平衡+左右深度差小于等于1
bool isBalanced(TreeNode *root)
{

	if (!root)//空节点也平衡
		return true;
	if (isBalanced(root->left) && isBalanced(root->right) && abs(findDepth(root->left) - findDepth(root->right)) <= 1)
		return true;
	return false;
}

void main()
{
	TreeNode* t1 = new TreeNode(1);
	TreeNode* t12 = new TreeNode(2);
	TreeNode* t22 = new TreeNode(2);
	TreeNode* t13 = new TreeNode(3);
	TreeNode* t23 = new TreeNode(3);
	TreeNode* t14 = new TreeNode(4);
	TreeNode* t24 = new TreeNode(4);

	t1->left = t12;
	t12->left = t13;
	t13->left = t14;
	t1->right = t22;
	t22->right = t23;
	t23->right = t24;

	cout << isBalanced(t1) << endl;
	cout << findDepth(t12) << endl;
	system("pause");
}
时间: 2024-12-17 22:56:07

110.Balanced Binary Tree的相关文章

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 递归求解

题目链接: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 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 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

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(平衡二叉树)(*)

翻译 给定一个二叉树,决定它是否是高度平衡的. (高度是名词不是形容词-- 对于这个问题.一个高度平衡二叉树被定义为: 这棵树的每一个节点的两个子树的深度差不能超过1. 原文 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

110. Balanced Binary Tree Java Solutions

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. Subscribe to see which companies as

[leedcode 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. /** * Definition for a binary tree

Java for 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. 解题思路: 递归即可,JAVA实现如下: public boolean