LeetCode110 Blanced Binary Tree Java 题解
来源:互联网 发布:耳鸣 知乎 编辑:程序博客网 时间:2024/06/06 15:34
题目:
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.
题解:
判断一颗二叉树是不是平衡二叉树 ,平衡二叉树是每个节点都满足指左右子树的高度差小于1
我们通过计算每一个节点的左右高度差 一旦发现有不满足的节点就将返回值置为-1 这两句代码if(left==-1) return -1;
if(right==-1) return -1保证只要出现一个高度差为-1的最终返回结果必然为-1,也就是说最终可以根据返回值是不是-1判断是不是平衡二叉树
代码:
public static boolean isBalanced(TreeNode root) {if(treeHight(root)==-1)return false;else return true; }public static int treeHight(TreeNode root){if(root==null)return 0;else {int left=treeHight(root.left);int right=treeHight(root.right);if(left==-1) return -1;if(right==-1) return -1;if(Math.abs(left-right)>1)return -1;else {return 1+Math.max(treeHight(root.left), treeHight(root.right));}}}
0 0
- LeetCode110 Blanced Binary Tree Java 题解
- LeetCode110.Balanced Binary Tree题解
- LeetCode110 - Balanced Binary Tree
- LeetCode110-Balanced Binary Tree
- leetcode110---Balanced Binary Tree
- LeetCode110:Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- leetcode110~Balanced Binary Tree
- leetcode110. Balanced Binary Tree
- LeetCode110 Balanced Binary Tree
- leetcode110 Balanced Binary Tree
- Leetcode110. Balanced Binary Tree
- LeetCode110. Balanced Binary Tree
- LeetCode110—Balanced Binary Tree
- LeetCode110——Balanced Binary Tree
- JAVA对SBTree(size blanced tree)的实现尝试
- LeetCode102 Binary Tree Level Order Traversal Java题解
- LeetCode 226. Invert Binary Tree 题解 —— Java
- Tools - recon-ng
- hdu 2551 竹青遍野
- Codeforces Round #229 (Div. 2)C. Inna and Candy Boxes
- GIT对VS工程中的无用文件过滤
- NYOJ 124 中位数(水题,nth_element()使用练习)
- LeetCode110 Blanced Binary Tree Java 题解
- C#课程设计:《猜猜看》游戏开发总结
- hdu 1710 Binary Tree Traversals 前序遍历和中序推后序
- Tomcat 启动 异常 java.lang.IllegalStateException: Unable to complete the scan for annotations...
- Android V4包下DrawerLayout的学习及回顾
- Could not find class 'javax.naming.directory.InitialDirContext'的解决办法!
- 操作系统——知识点,填空题
- Python学习笔记
- intellij idea + bitbucket + maven + spring-boot配置记录