`
testforvln
  • 浏览: 18952 次
  • 性别: Icon_minigender_1
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论
  • superich2008: 1、去重可以用Set集合2、在排序后,相邻2个元素如果相同可以 ...
    4Sum

Balanced Binary Tree

 
阅读更多
/**
 * 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) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (root == null)
            return true;
        else {
            if (!isBalanced(root.left) || !isBalanced(root.right))
                return false;
            else if (Math.abs(getDepth(root.left) - getDepth(root.right)) <= 1)
                    return true;
                else
                    return false;
        }
    }
    
    public int getDepth(TreeNode root){
        if (root == null)
            return 0;
        else 
            return Math.max(getDepth(root.left), getDepth(root.right)) + 1;
    }
}

小测试过了 大测试超时= = 哎。。。递归果然就是时间效率不高
想了一下发现是isBalanced中已经用isBalanced算过一次深度了 之后又算一次深度 囧

/**
 * Definition for binary tree
 * public class TreeNode {
 *     int val;
 *     TreeNode left;
 *     TreeNode right;
 *     TreeNode(int x) { val = x; }
 * }
 */
import java.util.HashMap;
public class Solution {
    static HashMap<TreeNode, Integer> depths = new HashMap<TreeNode, Integer>();
    public boolean isBalanced(TreeNode root) {
        // Start typing your Java solution below
        // DO NOT write main() function
        if (root == null)
            return true;
        else {
            if (!isBalanced(root.left) || !isBalanced(root.right))
                return false;
            else if (Math.abs(getDepth(root.left) - getDepth(root.right)) <= 1)
                    return true;
                else
                    return false;
        }
    }
    
    public int getDepth(TreeNode root){
        if (root == null)
            return 0;
        else 
            if (depths.containsKey(root))
                return depths.get(root);
            else{
                depths.put(root, Math.max(getDepth(root.left), getDepth(root.right)) + 1);
                return depths.get(root);
            }
    }
}


啊 然后写了一个HashMap存了一下深度就好了。。。
分享到:
评论

相关推荐

    leetcode的题目:Balanced Binary Tree

    leetcode的题目:Balanced Binary Tree

    Balanced-binary-tree.rar_Balanced Binary Tree

    手动输入数据,加入到二叉树中,并生成平衡二叉树,对学习C++及数据结构有较大帮助

    cpp-算法精粹

    Balanced Binary Tree Flatten Binary Tree to Linked List Populating Next Right Pointers in Each Node II 二叉树的构建 Construct Binary Tree from Preorder and Inorder Traversal Construct Binary Tree from...

    二叉树的应用:二叉排序树BST和平衡二叉树AVL的构造 课设作业 完整代码

    平衡二叉树(Balanced Binary Tree)又被称为AVL树。具有以下性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。构造与调整方法。 实验目的: 二叉排序树的实现...

    c语言平衡二叉树代码示例

    平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),它具有以下性质: 1. 它是一棵空树或它的左右两个子树的高度差的绝对值不超过1。 2. 左右两个子树都是一棵平衡二叉树。 平衡二叉树大部分操作和...

    平衡二叉树的所有操作(全)

    平衡二叉树(Balanced Binary Tree),简称平衡树(AVL树):树上任一结点的左子树和右子树的高度之差不超过1。平衡二叉树的一系列操作:删除、插入(在二叉排序树中插入新结点后,如何保持平衡)、调整平衡等等等。...

    左程云leetcode-algorithm-and-data-structure:算法+数据结构=程序

    树(Balanced Binary Tree/AVL Tree) 红黑树(Red-Black Tree) 伸展树(Splay Tree) B-树(B-Tree) 线索二叉树(Threaded Binary Tree) 前缀树/字典树(Trie) 5. 哈希/散列(Hashing) 哈希表(Hash Table) ...

    左程云leetcode-algorithm-and-data-structure:算法+数据结构=程序https://en.wikipedia

    树(Balanced Binary Tree/AVL Tree) 红黑树(Red-Black Tree) 伸展树(Splay Tree) B-树(B-Tree) 线索二叉树(Threaded Binary Tree) 前缀树/字典树(Trie) 5. 哈希/散列(Hashing) 哈希表(Hash Table) ...

    LeetCode最全代码

    * [Binary Search Tree](https://github.com/kamyu104/LeetCode#binary-search-tree) * [Breadth-First Search](https://github.com/kamyu104/LeetCode#breadth-first-search) * [Depth-First Search]...

    平衡二叉树算法详细解释

    形态匀称的二叉树称为平衡二叉树 (Balanced binary tree) ,其严格定义是:一棵空树是平衡二叉树;若 T 是一棵非空二叉树,其左、右子树为 TL 和 TR ,令 hl 和 hr 分别为左、右子树的深度。

    map-for-c-by-balanced-binary-tree

    基于平衡二叉树的C语言MAP,可以根据用户传入的类型自定义KEY和VAL的类型,如int or 结构体

    LintCode:针对LintCode问题的Java解决方案

    [Balanced Binary Tree.java]( Binary Tree.java) Java 7 [最佳买卖股票I.java]( 最佳买卖股票I.java) Java 8 [最佳买卖股票II.java]( 最佳买卖股票II.java) Java 9 [最佳买卖股票III.jav

    leetcode和oj-interviewprep:面试准备

    balanced binary tree, red/black tree splay tree AVL tree Implementation 图表 There are 3 basic ways to represent a graph in memory (objects and pointers, matrix, and adjacency list) B

    leetcode分类-LeetCode:力码

    leetcode 分类 LeetCode Progress 128/154 Other Solutions C++,有详细思路解释 python,部分有解释 Java,部分有解释 ...norvig神牛Python代码写的很飘逸,果然是有LISP内功的人!...Balanced Binary Tree Binar

    Balanced-Binary-Tree.zip_site:www.pudn.com

    基本实现平衡二叉树的创建,插入和删除操作。使用C++代码实现

    AVLTree.rar_数据结构_Visual_C++_

    数据结构,平衡二叉树平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树。...

    binary_trees

    二叉树 学习目标 What is a binary tree What is the difference between a binary tree and a Binary ...What is a complete, a full, a perfect, a balanced binary tree 数据结构 基本二叉树 /** * struct binary

    平衡二叉树详解及java代码的实现

    平衡二叉树(Balanced Binary Tree)又被称为AVL树(有别于AVL算法),且具有以下性质:它是一 棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树(递归定义)的二叉排序树。...

    二叉排序树与平衡二叉树的实现

    ①平衡二叉树(Balanced Binary Tree)是指树中任一结点的左右子树的高度大致相同。 ②任一结点的左右子树的高度均相同(如满二叉树),则二叉树是完全平衡的。通常,只要二叉树的高度为O(1gn),就可看作是平衡的。 ...

Global site tag (gtag.js) - Google Analytics