问题:给一个二叉树,写一个算法判断这个树是不是balanced。 Solution #1. 第一次遇到这个问题时我的解法,如下: 写了一个getDepth()函数,访问每个节点都要调用一次这个函数。这个Solution也通过了leetcode的验证程序,但是后来想了想,I ...
原题地址:http: oj.leetcode.com problems balanced binary tree 题意:判断一颗二叉树是否是平衡二叉树。 解题思路:在这道题里,平衡二叉树的定义是二叉树的任意节点的两颗子树之间的高度差小于等于 。这实际上是AVL树的定义。首先要写一个计算二叉树高度的函数,二叉树的高度定义为:树为空时,高度为 。然后递归求解:树的高度 max 左子树高度,右子树高度 ...
2014-05-10 10:54 0 5388 推荐指数:
问题:给一个二叉树,写一个算法判断这个树是不是balanced。 Solution #1. 第一次遇到这个问题时我的解法,如下: 写了一个getDepth()函数,访问每个节点都要调用一次这个函数。这个Solution也通过了leetcode的验证程序,但是后来想了想,I ...
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 ...
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 ...
Given a binary tree, determine if it is height-balanced. For this problem, a height-balanced binary tree is defined as: a binary tree ...
for binary tree * struct TreeNode { * int val; ...
原题地址:http://oj.leetcode.com/problems/binary-tree-inorder-traversal/ 题意:二叉树的中序遍历。这道题用递归比较简单,考察的是非递归实现二叉树中序遍历。中序遍历顺序为:左子树,根,右子树。如此递归下去。 解题思路:假设树为: 1 / \ ...
原题地址:http://oj.leetcode.com/problems/binary-tree-preorder-traversal/ 题意:这题用递归比较简单。应该考察的是使用非递归实现二叉树的先序遍历。先序遍历的遍历顺序是:根,左子树,右子树。 解题思路:如果树为下图: 1 / \ ...
原题地址:http://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题意:实现后序遍历。递归实现比较简单,非递归实现。 解题思路:这道题的迭代求解比先序遍历和后序遍历要麻烦一些。假设一棵树是这样的: 1 / \ ...