LeetCode OJ-- Balanced Binary Tree ***
https://oj.leetcode.com/problems/balanced-binary-tree/
判断一个二叉树,是否为平衡的。如果是平衡的,则它的每个子树的左右子树高度差不大于1.
递归
但是有两个值,在高层都需要知道。一个是子树是否为二叉树的 bool,一个是子树的高度。所以,一个作为返回值,一个作为引用给传到上面来。
class Solution {
public:
bool isBalanced(TreeNode *root) {
if(root == NULL)
return true;
int p;
return getDepth(root,p);
}
bool getDepth(TreeNode *root, int &depth)
{
if(root == NULL)
{
depth = ;
return true;
}
if(root->left == NULL && root->right == NULL)
{
depth = ;
return true;
}
int l, r;
if(getDepth(root->left, l) == false)
return false;
if(getDepth(root->right, r) == false)
return false;
if(abs(l-r)>)
return false;
else
{
//此时,depth需要传到上层去
depth = max(l,r) + ;
return true;
}
}
};
LeetCode OJ-- Balanced Binary Tree ***的更多相关文章
- C++版 - 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
剑指offer 面试题39:判断平衡二叉树 提交网址: http://www.nowcoder.com/practice/8b3b95850edb4115918ecebdf1b4d222?tpId= ...
- 【LEETCODE OJ】Binary Tree Postorder Traversal
Problem Link: http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ The post-order-traver ...
- 【LeetCode】Balanced Binary Tree 算法优化 解题报告
Balanced Binary Tree Better Solution [LeetCode] https://leetcode.com/submissions/detail/40087813/ To ...
- 【leetcode】Balanced Binary Tree(middle)
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- [LeetCode] 110. Balanced Binary Tree ☆(二叉树是否平衡)
Balanced Binary Tree [数据结构和算法]全面剖析树的各类遍历方法 描述 解析 递归分别判断每个节点的左右子树 该题是Easy的原因是该题可以很容易的想到时间复杂度为O(n^2)的方 ...
- 【LeetCode OJ】Binary Tree Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal/ Traverse the tree ...
- 【LeetCode OJ】Binary Tree Zigzag Level Order Traversal
Problem Link: https://oj.leetcode.com/problems/binary-tree-zigzag-level-order-traversal/ Just BFS fr ...
- 【LeetCode OJ】Binary Tree Level Order Traversal II
Problem Link: https://oj.leetcode.com/problems/binary-tree-level-order-traversal-ii/ Use BFS from th ...
- LeetCode之Balanced Binary Tree 平衡二叉树
判定一棵二叉树是不是二叉平衡树. 链接:https://oj.leetcode.com/problems/balanced-binary-tree/ 题目描述: Given a binary tree ...
- 【LeetCode OJ】Binary Tree Maximum Path Sum
Problem Link: http://oj.leetcode.com/problems/binary-tree-maximum-path-sum/ For any path P in a bina ...
随机推荐
- Python知识点入门笔记——特色数据类型(元组)
元组(tuple)是Python的另一种特色数据类型,元组和列表是相似的,可以存储不同类型的数据,但是元组是不可改变的,创建后就不能做任何修改操作. 创建元组 用逗号隔开的就是元组,但是为了美观和代码 ...
- Cube HDU - 1220(思维)
Cowl is good at solving math problems. One day a friend asked him such a question: You are given a c ...
- 动态规划:HDU2159-FATE(二维费用的背包问题)
FATE Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submi ...
- python集成开发环境PyCharm
环境安装视频介绍:http://pan.baidu.com/s/1gfz6wiZ ppmb 外加几个截图: activate:
- springMVC中接收数组参数
方式一. 后台:public ResultBean queryItemRulesByItemIds(int userId, int[] itemIds) 方式二.
- 洛谷P1067 多项式输出
题目链接:https://www.luogu.org/problemnew/show/P1067 这是一个纯模拟的小怪但是需要注意一些小细节: 1.首项为正没有+号. 2.所有项系数如果是一的话就省略 ...
- This application has request the Runtime to terminate it in an unusual way.
Q: CertsMV.exe gui popup two dialogs as follow. A: 测试发现是分配内存导致,频繁分配内存(大约6M) 可能是堆管理导致 分配大内存分配失败,程序未对 ...
- 【Word Break II】cpp
题目: Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where e ...
- IOS开发学习笔记010-面向对象的三大特性
面向对象的三大特性 1.封装 2.继承 3.多态 一.封装 将类内部的属性保护起来,在外部不能直接访问,那么如果需要访问怎么办呢? OC提供了set方法来对成员变量进行访问 set方法 1.作用:提供 ...
- day05_10 作业
bug1.0版本 #购物车程序 salary = int(input("请输入你的工资")) item = ['iphone6s','macbook','coffee','pyth ...