Leetcode: Balanced Binary Tree
来源:互联网 发布:mysise php爬虫 编辑:程序博客网 时间:2024/06/08 05:49
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.
最straight-forward的想法:
/** * Definition for binary tree * 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 (isBalanced(root->left) && isBalanced(root->right)){ int left_depth = maxDepth(root->left); int right_depth = maxDepth(root->right); if (abs(left_depth - right_depth) <= 1) { return true; } } return false; } int maxDepth(TreeNode *root) { if (root == NULL) { return 0; } return (max(maxDepth(root->left), maxDepth(root->right)) + 1); }};
检查平衡之后还要计算深度,有些复杂,虽然能通过。能集成到一起吗?
class Solution {public: bool isBalanced(TreeNode *root) { int depth = 0; return checkBalance(root, depth); } bool checkBalance(TreeNode *root, int& depth) { if (root == NULL) { depth = 0; return true; } int left_depth = 0; int right_depth = 0; if (checkBalance(root->left, left_depth) && checkBalance(root->right, right_depth)) { if (abs(left_depth - right_depth) > 1) { return false; } else { depth = max(left_depth, right_depth) + 1; return true; } } return false; }};
奇怪的是运行时间竟然一样。。。
=======================第二次===========================
/** * Definition for binary tree * 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 depth; return balance_util(root, depth); } bool balance_util(TreeNode* root, int& depth) { if (root == NULL) { depth = 0; return true; } int left_depth, right_depth; if (balance_util(root->left, left_depth) && balance_util(root->right, right_depth)) { if (abs(left_depth - right_depth) > 1) { return false; } else { depth = max(left_depth, right_depth) + 1; return true; } } return false; }};
0 0
- LeetCode: Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [Leetcode] Balanced Binary Tree
- LeetCode : Balanced Binary Tree
- LeetCode Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- [LeetCode]Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- [Leetcode]Balanced Binary Tree
- [leetcode]Balanced Binary Tree
- Leetcode: Balanced Binary Tree
- LeetCode-Balanced Binary Tree
- LeetCode - Balanced Binary Tree
- [LeetCode] Balanced Binary Tree
- LeetCode | Balanced Binary Tree
- 【leetcode】Balanced Binary Tree
- 图像腐蚀
- 基于OpenCV的读取摄像头实现单个人脸验证MFC程序
- 复制数据结构 - 单链表及图
- android ndk知识汇总——1
- wpa_supplicant更新问题
- Leetcode: Balanced Binary Tree
- Java Web获取Web应用根路径
- Hadoop版本选择探讨
- 使用JMF实现音乐播放(java多媒体编程)
- 析构函数不能抛出异常
- 一起做开源在线IDE(mokide=nodejs+jQueryUI+CodeMirror)
- google的高级搜索技巧
- 第十七周-项目五(1)百钱百鸡。
- 黑马程序员-java面向对象三大特性