Balanced Binary Tree(Java代码没有结束,是什么原因???)
2023-09-14 09:08:09 时间
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.
/** * 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=0; return isBalanced(root,&depth); } bool isBalanced(TreeNode *root,int* depth){ if(root==NULL){ *depth=0; return true; } int left,right; if(isBalanced(root->left,&left) && isBalanced(root->right,&right)){ int diff=left-right; if(diff<=1 && diff>=-1){ *depth=1+(left>right?left:right); return true; } } return false; } };相同思路的Java代码没过。谁能告诉我问题所在。由于參数的问题吗?
/** * Definition for binary tree * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */ public class Solution { public boolean isBalanced(TreeNode root) { int depth=0; return isBalanced(root,depth); } public boolean isBalanced(TreeNode node,int depth){ if(node == null){ return true; } int leftDepth=0,rightDepth=0; if( isBalanced(node.left,leftDepth) && isBalanced(node.right,rightDepth) ){ int diff=leftDepth-rightDepth; if(diff<=1 && diff>=-1){ depth=1+((leftDepth>rightDepth)?(leftDepth):(rightDepth)); return true; } } return false; } }
版权声明:本文博客原创文章,博客,未经同意,不得转载。
相关文章
- 痛苦的 java.net.BindException: Address already in use: connect —— Nacos的坑
- Java面向对象基础--代码块
- [转]关于JAVA中RSA加签解签,私钥加密公钥解密和公钥加密私钥解密代码详解
- Java实现蓝桥杯第十一届校内模拟赛
- Java实现 LeetCode 492 构造矩形
- Java GUI 鼠标事件
- java实现蓝桥杯约瑟夫环
- java算法集训代码填空题练习1
- Java中Iterator 和ListIterator的区别
- Java实现 蓝桥杯VIP 算法训练 快速排序
- (Java实现) 子集和问题
- Java 项目UML反向工程转化工具
- 【JAVA】eclipse里代码整个前移或者后移的快捷键
- java juc多线程编程代码示例:ForkJoinPool
- 项目里用到 Java 集合ArrayList和LinkedList时,应该避免的一些坑!
- Ian在北京做的CFCA的Java代码
- Atitti.java android反编译解决方案-----虚拟机方案
- Atitit.uml2 api 的编程代码实现设计uml开发 使用eclipse jar java 版本
- JAVA代码发送邮件示例和解释(二)
- 如何使用Java构建可扩展的数据处理管道?附完整示例代码
- OkHttp报错:java.lang.IllegalStateException: closed
- Simple Logging Facade for Java (SLF4J)作用(java日志框架)
- java推断字符串中是否包括字母
- java入门 常见问题0012:java中类中只能是成员变量定义,方法定义、语句块,具体的语句都应当放在方法或代码块中