[leetcode]_Balanced Binary Tree

第四道树题,逐渐能写递归了。虽然最后AC的代码还是看了网络,但是距离成功攻克此类问题仅一步之遥。

题目:一棵树,判断是否为AVL。(AVL条件:树上任意一点的左右子树的高度差<=1)

思路:树依旧用递归的思想。

解题历程:

1、凭感觉,依葫芦画瓢之前看的递归程序。“判断一颗树是否AVL,如果该node
!= null ,则递归地判断其 leftSubTree 和
rightSubTree是否为AVL。(这个地方错了,在第三块代码中反映出来,不是node != null ,应该是如果node结点是平衡的 ,
则递归判断其左右子树是否平衡)

public boolean isBalanced(TreeNode root) {
if(root == null) return true;

else return isBalanced(root.left) && isBalanced(root.right);
}

我只写到了这步,但是但是,这个递归程序只能返回true啊,任意一棵树都会返回true。华丽丽地WA。

2、思考是哪里错了,是否直接用isBalanced(root.left)和isBalanced(root.right)不对呢?是否应该在求高度时用递归呢。

一个结点的高度 = MAX(该结点左子树高度 , 该结点右子树高度) +
1;如果左右子树高度差绝对值<= 1 ,返回true,反之。

至此,我修改了程序:


public boolean isBalanced(TreeNode root) {
if(root == null) return true;
else return Math.abs(calHeight(root.left) - calHeight(root.right)) <= 1 ? true : false ;

}

public int calHeight(TreeNode node) {
if(node == null) return 0;
else return Math.max(calHeight(node.left) , calHeight(node.right)) + 1;
}

Submit。依旧WA。问题出在isBalance()中计算高度,我只计算了根节点root的左右子树的高度,左右子树中任一结点的高度差是否大于1无从得知。

3、上述代码的修改意见,应该就是加上各个结点的高度差判断,但是,我不会写了。%>_<%。网络上的AC代码:


public boolean isBalanced(TreeNode root) {
if(root == null) return true;

if(Math.abs(calHeight(root.left) - calHeight(root.right)) > 1 ) return false;

else return isBalanced(root.left) && isBalanced(root.right);
}

public int calHeight(TreeNode node) {
if(node == null) return 0;
else return Math.max(calHeight(node.left) , calHeight(node.right)) + 1;
}

惊奇地发现,就是我1,2版本代码的并集。再理下思路:

当一棵树root结点为Null时,该树为AVL。

判断该root结点是否平衡,如果不平衡,则直接返回false;如果平衡,则递归地判断该root结点的左右子树是否平衡,如果左右子树都平衡,才返回true。

时间: 2024-08-25 07:13:12

[leetcode]_Balanced Binary Tree的相关文章

leetcode第一刷_Balanced Binary Tree

二叉平衡树好火啊,几乎每个公司的笔试题里都有它,考了好多次我都不会,挂笔试很有可能就是因为它,还有一个它的同伙叫二叉搜索树,貌似人气比它还要高一些.二叉平衡树是什么样的树呢,是每个节点的左右子树高度相差绝对值都不超过1.好,你说你终于回了,这不很简单吗,求一下根节点的左右字数高度,如果满足,他就是,否则就不是嘛.不是啊亲,要求是所有节点都满足这个条件,判断的时候必须每个节点都验证的! 扯了这么长,其实看看代码就明白了,怎么有种在贴吧发言要凑够15字的感觉. int getHeight(TreeN

LeetCode 145 Binary Tree Postorder Traversal(二叉树的后续遍历)+(二叉树、迭代)

翻译 给定一个二叉树,返回其后续遍历的节点的值. 例如: 给定二叉树为 {1, #, 2, 3} 1 2 / 3 返回 [3, 2, 1] 备注:用递归是微不足道的,你可以用迭代来完成它吗? 原文 Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recur

[LeetCode] Construct Binary Tree from Inorder and Postorder Traversal

Given inorder and postorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. class Solution { public: TreeNode *buildTree(vector<int> &inorder, vector<int> &postorder) { int

leetcode -day29 Binary Tree Inorder Traversal &amp; Restore IP Addresses

1.  Binary Tree Inorder Traversal Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [1,3,2]. Note: Recursive solution is trivial, could you do it iteratively? 分析:求二叉树的中序

LeetCode OJ - Binary Tree Level Order Traversal 1 &amp;&amp; 2

BFS以及它的扩展,我发现栈是个很好用的数据结构,特别是对于顺序需要颠倒的时候!!! 这里有个重要的信息:可以用null来标识一个level的结束!!! 下面是AC代码: 1 /** 2 * Given a binary tree, return the bottom-up level order traversal of its nodes' values. 3 * (ie, from left to right, level by level from leaf to root). 4 *

leetcode day4 -- Binary Tree Postorder(Preorder) Traversal &amp;&amp; Edit Distance

 1.Binary Tree Postorder Traversal Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 分析:后续遍历

LeetCode:Binary Tree Paths - 获取一棵树从顶点到每个叶节点的路径

1.题目名称 Binary Tree Paths(获取一棵树从顶点到每个叶节点的路径) 2.题目地址 https://leetcode.com/problems/binary-tree-paths/ 3.题目内容 英文:Given a binary tree, return all root-to-leaf paths. 中文:给定一颗二叉树,返回所有的根节点到叶节点的路径 例如:现有一颗二叉树    1  /   2     3    5 所有由根节点到叶节点的路径如下: ["1->2-

[LeetCode 题解]: Binary Tree Preorder Traversal

Given a binary tree, return the preorder traversal of its nodes' values. For example:Given binary tree {1,#,2,3}, 1 2 / 3 return [1,2,3]. Note: Recursive solution is trivial, could you do it iteratively? 题意 先序遍历二叉树,递归的思路是普通的,能否用迭代呢? 非递归思路:<借助stack>

【Leetcode】Binary Tree Postorder Traversal

Given a binary tree, return the postorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, 1 2 / 3 return [3,2,1]. Note: Recursive solution is trivial, could you do it iteratively? 思路:后序遍历比起先序遍历以及中序遍历要稍微复杂一点,可以考虑用两个stack进行操作,