b*******n 发帖数: 847 | 1 谢谢!
经过你提醒我改了我的code,这回过了,发现用array的index来referecen简单多了
/**
* Definition for binary tree
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
vector > levelOrderBottom(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > traversal;
vector path;
vector阅读全帖 |
|
b*******n 发帖数: 847 | 2 如题,我想实现先把整个tree breadth-first traverse,traverse结果放在vector里
(vector allPath)
,每层遍历到了最后加一个tag node,最后把vector从后往前scan。想法很简单,但
code总是有错。我对vector不熟,估计是member function没用对,请大家帮忙看看,
多谢了!
vector > levelOrderBottom(TreeNode *root) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
vector > traversal;
vector allPath;
if(!root) return traversal;
TreeNode *tag=new TreeNode(0);
al... 阅读全帖 |
|
d**e 发帖数: 6098 | 3
不知我有没有理解错,我觉得这个if有可能导致死循环。
比如是
1
/ \
2 3
开始时allPath: 1, tag
it指向1时,allPath: 1, tag, 2, 3
it指向tag时,allPath: 1, tag, 2, 3, tag
it指向2,3时不变,仍是allPath: 1, tag, 2, 3, tag
it指向最后一个tag时,加一个tag,allPath: 1, tag, 2, 3, tag, tag
再指下一个tag,再加一个tag,继续指到下一个tag。。。
right |
|
a********n 发帖数: 1287 | 4 #include
using namespace std;
// 0 means right, 1 means down
void AllPath(int N, int row, int col, int path[], int idx) {
if(row==N-1 && col==N-1) {
for(int i=0; i<2*N-2; i++) {
cout << path[i] << " ";
}
cout << endl;
}
// go down
if(row != N-1) {
path[idx] = 1;
AllPath(N, row+1, col, path, idx+1);
}
// go right
if(col != N-1) {
path[idx] = 0;
AllPath(N, row, col+1, path, idx+1);
}
}
in... 阅读全帖 |
|
e*******i 发帖数: 56 | 5 The last solution has a bug, it will loop forever.
Following is tested to work.
/*****************************************/
int A[4][4]={
{1,1,1,1},
{0,1,1,1},
{0,1,1,1},
{0,0,0,1}
};
struct Delta
{
int dx;
int dy;
};
Delta d[4]={
{0,1}, {1,0}, {-1, 0}, {0,-1} };
void dfs(vector< pair > &edgeTo, int *A, int m, int n, int i, int
j)
{
if( i>=m || j>=n || *(A+i*m+j)!=1 ) return;
edgeTo.push_back( pair(i,... 阅读全帖 |
|
x*******a 发帖数: 11067 | 6 (UNC path is what you see when you use your network neighbours.
Its usually in the form of \\theOtherComputer\sharedDirectory\ )
I can copy and remove files to a UNC path, however, when I try to
execute the copied executable, Matlab gives error:
??? Error using ==> dos
DOS commands may not be executed when the current directory is a UNC
pathname.
The following is the code:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
allpath='\\node2\share\temp '
cd(allpath)
copyfile E:\DUMMY.EXE
! DUMMY.EXE
%%%%%%%%%%%%%%%%% |
|
|
|
p*****2 发帖数: 21240 | 9 第一题这样可以吗?
public class Game
{
private void FindPath(int total, int x, int level, List path)
{
if (path.Count == x)
{
foreach (int i in path)
Console.Write("{0} ", i);
Console.WriteLine();
return;
}
for (int i = level; i < total; i++)
{
path.Add(i);
FindPath(total, x,i + 1, path);
path.RemoveAt(path.Count - 1);
}
}
public void AllPaths(int x,... 阅读全帖 |
|
l******s 发帖数: 3045 | 10 两个Queue也可以吧,Queue 和 Queue,保持两个Queue同步就行了
。
It's accepted by leetcode.
private IList allPath(TreeNode root){
Queue queue = new Queue();
Queue qStr = new Queue();
IList result = new List();
if(root == null) return result;
queue.Enqueue(root); qStr.Enqueue(root.val.ToString());
while(queue.Count != 0){
TreeNode cur = queue.Dequeue();
string curStr = qStr.Dequeue();
if(cur.le... 阅读全帖 |
|