原文:[leetcode]Balanced Binary Tree @ Python

原题地址:http: oj.leetcode.com problems balanced binary tree 题意:判断一颗二叉树是否是平衡二叉树。 解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于 。这实际上是AVL树的定义。首先要写一个计算二叉树高度的函数,二叉树的高度定义为:树为空时,高度为 。然后递归求解:树的高度 max 左子树高度,右子树高度 ...

2014-05-10 10:54 0 5388 推荐指数:

查看详情

[Leetcode] Balanced Binary Tree

问题:给一个二叉树,写一个算法判断这个树是不是balanced。 Solution #1. 第一次遇到这个问题时我的解法,如下: 写了一个getDepth()函数,访问每个节点都要调用一次这个函数。这个Solution也通过了leetcode的验证程序,但是后来想了想,I ...

Sun May 04 08:58:00 CST 2014 2 6363
LeetCode Balanced Binary Tree

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 ...

Sun Oct 28 06:40:00 CST 2012 5 4228
[面试真题] LeetCodeBalanced Binary Tree

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 ...

Sun May 12 05:21:00 CST 2013 0 2754
[leetcode]Binary Tree Inorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历。这道题用递归比较简单,考察的是非递归实现二叉树中序遍历。中序遍历顺序为:左子树,根,右子树。如此递归下去。 解题思路:假设树为:                 1                /  \             ...

Sat May 10 19:50:00 CST 2014 0 4451
[leetcode]Binary Tree Preorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单。应该考察的是使用非递归实现二叉树的先序遍历。先序遍历的遍历顺序是:根,左子树,右子树。 解题思路:如果树为下图:                       1                      / \     ...

Sat May 10 20:44:00 CST 2014 0 3861
[leetcode]Binary Tree Postorder Traversal @ Python

原题地址:http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题意:实现后序遍历。递归实现比较简单,非递归实现。 解题思路:这道题的迭代求解比先序遍历和后序遍历要麻烦一些。假设一棵树是这样的:                         1                        /  \       ...

Sun May 11 02:40:00 CST 2014 0 2827
 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM