題意:題目會提供一個數字n,和一個Binary Tree(二元樹),請你算出二元樹根(root)到葉(left)的路徑經過的node值總和。
想法:
input的地方:
1) ()
2) (int()())
3) (int(int()())(int()()))
有沒有發現一個規律,
"(" + "內容物" + ")"
對吧?
"(" + "內容物" + ")"
對吧?
所以輸入的地方可以寫成
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
int x; | |
char ch; | |
cin>>x; | |
cin>>ch;//讀"(" | |
cin>>x;//讀內容物 | |
if(!cin){ | |
cin.clear();//如果內容物不是int,就把剛剛讀的東西吐出來 | |
} | |
cin>>ch; // ch=='('; |
沒有留言:
張貼留言
注意:只有此網誌的成員可以留言。