leetcode_Balanced Binary Tree
来源:互联网 发布:js页面切换动画效果 编辑:程序博客网 时间:2024/05/10 06:45
class Solution {public: int diff=-1; int maxDiff(TreeNode * root) { int ldepth=0,rdepth=0; if(root==NULL) return 1; if(root->left!=NULL) ldepth=maxDiff(root->left)+1; if(root->right!=NULL) rdepth=maxDiff(root->right)+1; if(diff<ldepth-rdepth) diff=ldepth-rdepth; if(diff<rdepth-ldepth) diff=rdepth-ldepth; return ldepth>rdepth?ldepth:rdepth; } bool isBalanced(TreeNode* root) { if(root==NULL) return true; maxDiff(root); if(diff>=2) return false; return true; }};
题目链接:https://leetcode.com/problems/balanced-binary-tree/
要注意代码规范if(root->left)是错的,一定要写成if(root->left!=NULL)
0 0
- Leetcode_balanced-binary-tree
- LeetCode_Balanced Binary Tree
- leetcode_Balanced Binary Tree
- Binary tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- binary tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- Binary Tree
- 【Latex】Latex小白入门——如何迅速完成论文从word到Latex的移植
- Could not create ServerSocket on address 0.0.0.0/0.0.0.0:9083
- printStream 和printWriter区别
- 立体校正
- 华为P9手机的品质还需工匠精神
- leetcode_Balanced Binary Tree
- 基于注解的Spring MVC+Hiberntae简单入门
- 13 Free Open Source NetFlow Analyzers for Windows and Linux/Unix
- 通过GridView仿微信动态添加本地图片
- MySql join语句
- android 使用Http的POST方式读取网络数据
- Topcoder SRM 688 div2
- 【C#设计模式-策略者模式】
- mkisofs命令制作光盘ISO