為了練習尋找實習時會碰到的問題,我用別人推薦的 LeetCode online judge。
希望能直接寫程式,不用事先編譯就能直接AC。
這是下面是我寫的 Binary Tree Preorder Traversal 的解法。
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
The following program is my Binary Tree Preorder Traversal solution.
編譯錯誤次數 Compile Error number: 0
嘗試次數 Try number: 2 (until first AC) (RE: 1)
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: Yes
RE原因: 使用vector時,用assign的方式,但是那時vector沒有實際大小,
改成push_back()就AC了~
點這裡看題目 Click here to see this Problem!
Recursive的版本實在太簡單了,這個是Iterative版本~(還是很簡單= =)
原本多寫了一個function來跑遞迴,上面的程式是兩個function。
後來改成用原本的遞迴就解決即可(下面的程式)。
If you want to use (copy, paste or quote) my original article, please contact me through email. (autek.roy@gmail.com) If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
希望能直接寫程式,不用事先編譯就能直接AC。
這是下面是我寫的 Binary Tree Preorder Traversal 的解法。
To practice future interview question, I use LeetCode online judge.
I am trying to type code directly on website without compile in other tool.
The following program is my Binary Tree Preorder Traversal solution.
編譯錯誤次數 Compile Error number: 0
嘗試次數 Try number: 2 (until first AC) (RE: 1)
是否事先在其他工具編譯 if compile first in other tool: No
使用的程式語言 using programming language: C++
以前是否看過 seen this problem before: Yes
RE原因: 使用vector時,用assign的方式,但是那時vector沒有實際大小,
改成push_back()就AC了~
點這裡看題目 Click here to see this Problem!
Recursive的版本實在太簡單了,這個是Iterative版本~(還是很簡單= =)
原本多寫了一個function來跑遞迴,上面的程式是兩個function。
後來改成用原本的遞迴就解決即可(下面的程式)。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
private: | |
vector<int> preOrder; | |
public: | |
vector<int> preorderTraversal(TreeNode *root) { | |
preOrder.clear(); | |
preTraversal(root); | |
return preOrder; | |
} | |
public: | |
void preTraversal(TreeNode *root) { | |
if(root == NULL) | |
return; | |
preOrder.push_back(root -> val); | |
preTraversal(root -> left); | |
preTraversal(root -> right); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Definition for binary tree | |
* struct TreeNode { | |
* int val; | |
* TreeNode *left; | |
* TreeNode *right; | |
* TreeNode(int x) : val(x), left(NULL), right(NULL) {} | |
* }; | |
*/ | |
class Solution { | |
private: | |
vector<int> preOrder; | |
public: | |
vector<int> preorderTraversal(TreeNode *root) { | |
if(root == NULL) | |
return preOrder;//just return, no experssion would accept this vector | |
preOrder.push_back(root -> val); | |
preorderTraversal(root -> left); | |
preorderTraversal(root -> right); | |
return preOrder; | |
} | |
}; |
If you want to use (copy, paste or quote) my original article, please contact me through email. (autek.roy@gmail.com) If there is any mistake or comment, please let me know. :D
如要使用(複製貼上或轉載)作者原創文章, 請來信跟我聯絡。(autek.roy@gmail.com) 如果有發現任何的錯誤與建議請留言或跟我連絡。 : )
zh-CN → zh-TW
次數
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!