LeetCode OJ: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 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) {
int dep;
checkBalance(root, dep);
}
bool checkBalance(TreeNode * root, int &dep)
{
if(root == NULL){
dep = ;
return true;
}
int leftDep, rightDep;
bool isLeftBal = checkBalance(root->left, leftDep);
bool isRightBal = checkBalance(root->right, rightDep); dep = max(leftDep, rightDep) + ;
return isLeftBal && isRightBal && (abs(leftDep - rightDep) <= );
}
};
pS:感觉这个不应该easy的题目啊 想的时候头还挺疼的。。
用java的时候用上面的方法去做总是无法成功,所以换了一种方法,这个一开始没有想到,是看别人写的,代码如下所示:
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
public class Solution {
public boolean isBalanced(TreeNode root) {
if(root == null)
return true;
if(root.left == null && root.right == null)
return true;
if(Math.abs(getDep(root.left) - getDep(root.right)) > 1)
return false;
return isBalanced(root.left) && isBalanced(root.right);
} public int getDep(TreeNode node){
if(node == null)
return 0;
else
return 1 + Math.max(getDep(node.left), getDep(node.right));
}
}
LeetCode OJ:Balanced Binary Tree(平衡二叉树)的更多相关文章
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- [LeetCode] 110. Balanced Binary Tree 平衡二叉树
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- 【LeetCode】Balanced Binary Tree(平衡二叉树)
这道题是LeetCode里的第110道题. 题目要求: 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点 的左右两个子树的高度差的绝对值不超过1. ...
- LeetCode 110. Balanced Binary Tree平衡二叉树 (C++)
题目: Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bin ...
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- [CareerCup] 4.1 Balanced Binary Tree 平衡二叉树
4.1 Implement a function to check if a binary tree is balanced. For the purposes of this question, a ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
随机推荐
- 画图-drawpoint and drawpath
版权声明:本文因海牛宝宝童鞋才疏学浅,可能晦涩难懂,但也是呕心沥血之作,麻烦转载的时候留个申明. https://blog.csdn.net/u012321815/article/details/26 ...
- linux list.h 移植
Linux内核中List链表的实现,对于想进阶的程序员来说,无疑是一个很好的学习机会.内核实现了一个功能十分强大的链表,而且是开源的,用在其他需要的地方岂不是很省事. 一.看List实现前,先补充ty ...
- Python基础-set集合
1.集合的创建 s = set('fansik and fanjinbao') print(s) 打印结果(去掉了重复的字符):{'k', 'd', 'f', 'n', ' ', 'j', 'i', ...
- 鸟哥的Linux私房菜-第一部分-第3章主机规划与磁盘分区
1. 选择一个与你的Linux搭配的主机配置 NAT服务器:小型企业或者学校都基本是只有一条对外的线路,网卡 SAMBA服务器:完成Windows网上邻居的功能,网卡和硬盘要求高 Mail服务器:如果 ...
- 2015.6.30 反弹的教训(想做T)
心路:在6.29号,市场连续大跌!我到6.29号才想到可以做T+0.6.30消息面已经利好(双降准),已经计划做T+0(X先买后卖). 开市大跌至跌停.午后所有股票开始反弹.但是上午跌停时不敢入市, ...
- image has dependent child images
在删除镜像之前要先用 docker rm 删掉依赖于这个镜像的所有容器(哪怕是已经停止的容器),否则无法删除该镜像. 停止容器 # docker stop $(docker ps -a | grep ...
- PHP中的常见魔术方法功能作用及用法实例
概述 在面向对象编程中,PHP提供了一系列的魔术方法,这些魔术方法为编程提供了很多便利.PHP中的魔术方法通常以__(两个下划线)开始,并且不需要显示的调用而是由某种特定的条件出发. 开始之前 在总结 ...
- 解决Newtonsoft.Json版本问题
在配置文件中添加以下代码,App.config或Web.config <runtime> <assemblyBinding xmlns="urn:schemas-micro ...
- java写出图形界面
1. 做出简单的窗体 package javaGUI; import java.awt.BorderLayout; import java.awt.Color; import javax.swing. ...
- Linux操作系统的安装以及基本的操作命令详解
背景:使用的虚拟机安装Linux 虚拟机使用的是VMware Linux版本:CentOS-6.7-X86 自行下载:CentOS-6.7-x86_64-bin-DVD1.iso 打开VMw ...