《剑指offer》第39题:平衡二叉树
来源:互联网 发布:编程原本 pdf 下载 编辑:程序博客网 时间:2024/05/21 09:45
代码:
class Solution {public: bool IsBalanced(TreeNode* pRoot, int* pDepth) { if (pRoot == NULL) { *pDepth = 0; return true; } int nLeft ; int nRight ; if (IsBalanced(pRoot->left, &nLeft) && IsBalanced(pRoot->right, &nRight)) { int dif = nLeft - nRight; if (abs(dif)<2) { *pDepth = 1 + ((nLeft > nRight) ? nLeft : nRight); return true; } } return false; } bool IsBalanced_Solution(TreeNode* pRoot) { int pDepth = 0; return IsBalanced(pRoot, &pDepth); }};
曾经在这段代码中出现的错误:
*pDepth = 1 + ((nLeft > nRight) ? nLeft : nRight);
这一行里,+
比? :
优先级高,因此需要用括号显示确定优先级。
0 0
- 《剑指offer》第39题:平衡二叉树
- 【剑指offer】题39:平衡二叉树
- 剑指offer-39-2 平衡二叉树
- 剑指offer 39 平衡二叉树
- 剑指offer--平衡二叉树
- 《剑指offer》平衡二叉树
- 剑指Offer--平衡二叉树
- 剑指offer-平衡二叉树
- 剑指offer:平衡二叉树
- 剑指offer 平衡二叉树
- 剑指offer:平衡二叉树
- [剑指offer]平衡二叉树
- 剑指offer|平衡二叉树
- 《剑指offer》-平衡二叉树
- 剑指offer-平衡二叉树
- 剑指offer: 平衡二叉树
- 剑指offer:平衡二叉树
- 剑指offer-平衡二叉树
- makefile
- HDU 1213 How Many Tables
- 《React-Native系列》5、RN实现弹出选择界面与动画效果
- 杭电 Problem 2008 分拆素数和 【打表】
- java中类名.class, class.forName(), getClass()区别
- 《剑指offer》第39题:平衡二叉树
- 最长回文串
- POJ 1724 ROADS
- 一个makefile使用实例
- hdoj 2098 分拆素数和 【素数打表】
- Java烧脑驴游(十四)--流(Stream)、文件(File)和IO
- 无线网覆盖
- 一个bat使用实例
- uboot 移植