"Binary Tree Level Order Traversal" and "Balanced Binary Tree"

原创 2016年06月01日 14:26:14

这两道题刚好分别是广度优先搜索和深度优先搜索,拿出来对比学习一下。

Binary Tree Level Order Traversal:

queue<TreeNode*> _1_q;

	vector<vector<int>> ovec;
	vector<int> ivec;

	TreeNode *temp = root;
	if (!temp)
		return ovec;

	_1_q.push(temp);

	while (!_1_q.empty())
	{
		queue<TreeNode*> _2_q;//_2_q的作用域是单次循环,每个循环结束后该变量消失,相当于变相清空了队列2
		while (!_1_q.empty())
		{
			temp = _1_q.front();
			ivec.push_back(temp->val);
			_1_q.pop();
			if (temp->left)
				_2_q.push(temp->left);
			if (temp->right)
				_2_q.push(temp->right);
		}
		ovec.push_back(ivec);//将内层循环求得的树的一层的节点记录到ovec中(按顺序)
		ivec.clear();//清理内层循环记录变量ivec
		_1_q = _2_q;//双队列的好处就是可以很方便地更新_1_q,为下一次循环做准备
	}

	return ovec;


Balanced Binary Tree:

上三份代码来解释DFS,有的是从网上找的。

/*通过递归遍历整棵树来得到树的深度。
遍历的过程是首先一直向左走,走到左子树的左子树的左....的左子树,
到达叶子节点后返回父节点,然后向父节点的右子树走,这样不断回溯来实现遍历。
这就是深度优先遍历。*/
int maxDepth(TreeNode* root) {
	int deep = 0;
	if (root) {
		int lchilddeep = maxDepth(root->left);
		int rchilddeep = maxDepth(root->right);
		deep = lchilddeep >= rchilddeep ? lchilddeep + 1 : rchilddeep + 1;
	}
	return deep;
}


/*对比上一份代码,这份代码在回溯到某个父节点的时候又向下用递归做了一次深度优先遍历(getDepth)
来得到该节点的左右子树的深度,变相地增加了时间复杂度*/
public bool isBalanced(TreeNode root) {
	if (root == null) {
		return true;
	}

	int depthOfLeft = getDepth(root.left, 1);
	int depthOfRight = getDepth(root.right, 1);

	if (abs(depthOfRight - depthOfLeft) > 1) {
		return false;
	}
	else {
		return isBalanced(root.left) && isBalanced(root.right);
	}
}

private int getDepth(TreeNode tree, int currentDepth) {
	if (tree == null) {
		return currentDepth;
	}
	return max(getDepth(tree.left, currentDepth + 1),
		getDepth(tree.right, currentDepth + 1));
}

/*这份代码只需进行一次深度优先遍历,计算每个节点左右子树的高度的差值,
差值大于1返回false,否则用一个map存储该节点左右子树的最高的那一棵子
树的高度并返回true。*/
map<TreeNode*, int> heights;
bool isBalanced(TreeNode *root)
{
	if (root == NULL)
	{
		heights.insert(pair<TreeNode*, int>(NULL, 0));
		return true;
	}

	if (isBalanced(root->left) && isBalanced(root->right))
	{
		auto _l_it = heights.find(root->left);
		auto _r_it = heights.find(root->right);

		if (abs(_l_it->second - _r_it->second) > 1)
		{
			return false;

		}
		else
		{
			int currentHeight = max(_l_it->second + 1, _r_it->second + 1);
			heights.insert(pair<TreeNode*, int>(root, currentHeight));
			return true;
		}

	}
	else
	{
		return false;
	}
}




版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

LeetCode题解:Binary Tree Level Order Traversal I and II

Binary Tree Level Order Traversal Given a binary tree, return the level order traversal of its node...

LeetCode Binary Tree Level Order Traversal I and II

题目: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left ...

LeetCode(102)Binary Tree Level Order Traversal

题目如下: Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to ...

LeetCode:Binary Tree Zigzag Level Order Traversal

使用队列实现二叉树遍历,本题的特殊地方是遍历queue的时候需要从前往后,从后往前替代遍历。 package leetcode; import java.util.ArrayList; impo...
  • fytain
  • fytain
  • 2013-08-25 15:44
  • 1471

[LeetCode]Binary Tree Zigzag Level Order Traversal

题目链接:Binary Tree Zigzag Level Order Traversal 题目内容: Given a binary tree, return the zigzag level o...

103. Binary Tree Zigzag Level Order Traversal

逐行换方向层序遍历二叉树,queue和stack的不用应用。

【LeetCode】103. Binary Tree Zigzag Level Order Traversal 解题报告

转载请注明出处:http://blog.csdn.net/crazy1235/article/details/51471280Subject 出处:https://leetcode.com/pro...

LeetCode102 Binary Tree Level Order Traversal Java

题目: Given a binary tree, return the level order traversal of its nodes’ values. (ie, from left to r...

leetcode[103]:Binary Tree Zigzag Level Order Traversal

Binary Tree Zigzag Level Order Traversal Given a binary tree, return the zigzag level order travers...

Binary Tree Level Order Traversal I II(102、107)

Binary Tree Level Order Traversal
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)