Leetcode dfs Binary Tree Postorder Traversal II

Sudoku Solver

Total Accepted: 11752 Total
Submissions: 56537My Submissions

Write a program to solve a Sudoku puzzle by filling the empty cells.

Empty cells are indicated by the character ‘.‘.

You may assume that there will be only one unique solution.

A sudoku puzzle...

...and its solution numbers marked in red.

题意:填充数独,返回是否填充成功

思路:dfs

找一个还没填充的位置,尝试填写0-9其中的一个数字,判断是否理。

如果合理,则可以转移到一个同样子问题,所以可以采用递归的方式实现。

bool solveSudoku(vector<vector<char> >&board)

返回是否成功填充当前状态为 board的数独

bool isValid(const vector<vector<char> >&board, int x, int y){
	//检查行
	for(int j = 0; j < 9; ++j) if(j != y && board[x][j] == board[x][y]) return false;
	//检查列
	for(int i = 0; i < 9; ++i) if(i != x && board[i][y] == board[x][y]) return false;
	//检查小方块
	for(int i = 0; i < 3; ++i)
		for(int j = 0; j < 3; ++j){
			if(!(x/3 * 3 + i == x && y /3 * 3 + j == y) && board[x/3 * 3 + i][y /3 * 3 + j] == board[x][y]) return false;
		}
	return true;
}

bool solveSudoku(vector<vector<char> >&board)
{
	for(int i = 0; i < 9;  ++i){
		for(int j = 0; j < 9; ++j){
			if(board[i][j] == '.'){
				for(int k = 0; k < 9; ++k){
					board[i][j] = k + '1' ;
					if(isValid(board, i, j) && solveSudoku(board)) return true;
					board[i][j] = '.';
				}
				return false;
			}
		}
	}
	return true; //漏写了这句,WA了好多次
}

时间: 2024-10-13 01:07:59

Leetcode dfs Binary Tree Postorder Traversal II的相关文章

Leetcode dfs Binary Tree Postorder Traversal

Binary Tree Postorder Traversal Total Accepted: 28560 Total Submissions: 92333My Submissions 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 s

Leetcode bfs&amp;dfs Binary Tree Postorder Traversal II

Binary Tree Level Order Traversal II Total Accepted: 16983 Total Submissions: 54229My Submissions Given a binary tree, return the bottom-up level order traversal of its nodes' values. (ie, from left to right, level by level from leaf to root). For ex

【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进行操作,

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? 说明: 1) 两种实现,递归与非递归 , 其中非递归有两种方法 2)复杂度分析:时

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 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][JavaScript]Binary Tree Postorder Traversal

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 Postorder Traversal (3 solutions)

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 bfs&amp;dfs Binary Tree Postorder Traversal

Binary Tree Level Order Traversal Total Accepted: 20571 Total Submissions: 66679My Submissions Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). For example: Given binary tree {3,9,2