LeetCode-Balanced Binary Tree
平衡二叉树的判断,DFS,
一直没想到怎么同时返回结点的长度和状态,把高度放到参数里面比较简便!
1 class Solution { 2 public: 3 bool isBalanced(TreeNode *root) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int height = 0; 7 return getHeight(root, height); 8 9 } 10 bool getHeight(TreeNode *root, int &height) { 11 if (!root) { 12 return true; 13 } 14 int leftheight = 0; 15 int rightheight = 0; 16 if (getHeight(root->left, leftheight) && getHeight(root->right, rightheight)) { 17 height = max(leftheight, rightheight) + 1; 18 if (abs(leftheight - rightheight) <= 1) { 19 return true; 20 } 21 } 22 return false; 23 } 24 };
不过其实不需要用两个变量来表示状态,可以直接用一个int返回值来表示,
如果是false的则直接返回-1,否则返回高度:
1 class Solution { 2 public: 3 bool isBalanced(TreeNode *root) { 4 // Start typing your C/C++ solution below 5 // DO NOT write int main() function 6 int ret = getHeight(root); 7 if (ret == -1) { 8 return false; 9 } 10 return true; 11 } 12 int getHeight(TreeNode *root) { 13 if (root == NULL) { 14 return 0; 15 } 16 int left = getHeight(root->left); 17 int right = getHeight(root->right); 18 if (left == -1 || right == - 1) { 19 return -1; 20 } 21 if (abs(left - right) > 1) { 22 return -1; 23 } 24 return (max(left, right) + 1); 25 } 26 };
转载于:https://www.cnblogs.com/chasuner/p/balanced.html
LeetCode-Balanced Binary Tree相关推荐
- leetcode - Balanced Binary Tree
题目:Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this problem, a ...
- LeetCode Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- leetcode -- Balanced Binary Tree TODO
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- Leetcode::Balanced Binary Tree
后序遍历,每个节点只遍历一次. 1 /** 2 * Definition for binary tree 3 * struct TreeNode { 4 * int val; 5 * TreeNode ...
- Leetcode: Balanced Binary Tree
Given a binary tree, determine if it is height-balanced.For this problem, a height-balanced binary t ...
- LeetCode:Balanced Binary Tree
题目链接 Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced bi ...
- [LeetCode]Balanced Binary Tree
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary ...
- LeetCode 110 Balanced Binary Tree 平衡二叉树
LeetCode 110 Balanced Binary Tree Given a binary tree, determine if it is height-balanced. For this ...
- LeetCode 110. 平衡二叉树(Balanced Binary Tree) 15
110. 平衡二叉树 110. Balanced Binary Tree 题目描述 给定一个二叉树,判断它是否是高度平衡的二叉树. 本题中,一棵高度平衡二叉树定义为: 一个二叉树每个节点的左右两个子树 ...
- LeetCode 110. Balanced Binary Tree
LeetCode 110. Balanced Binary Tree 本博客参考自:http://www.cnblogs.com/grandyang/p/4045660.html Solution1: ...
最新文章
- Google Colab——用谷歌免费GPU跑你的深度学习代码
- php 清空cache,到底如何清除 cache? 我真的快瘋了
- vue 加载时掉用mounted_Vue实例中生命周期created和mounted的区别详解
- pandas数据结构:Series/DataFrame;python函数:range/arange
- VTK:图像亮度用法实战
- MusicXML 3.0 (4) - 谱号
- [渝粤教育] 中国地质大学 信息资源管理 复习题 (2)
- Mac下使用Fiddler
- 【java】画图和监听事件的应用
- mysql在linux下的完整安装
- Kotlin基础学习第3章—内置类型
- 苹果电脑修改hosts文件方法介绍!mac的hosts文件位置在哪?
- 人力资源经理的选择(转载)
- python中颜色表_python 颜色表
- 移动网络安装测试软件,adsl网速测试(中国移动宽带专用测速软件)
- EasyCamera中海康摄像头语音对讲和云台控制转发实现
- html标签的记忆巧法,小学记忆单词的方法
- 利用pyhton爬虫(案例3)--X房网的小房子们
- 单招计算机网络考什么,单招考什么内容 单招一般考什么科目
- vue 实现前端excel导出表格携带token的两种方法