[問題] LeetCode Runtime Error

看板C_and_CPP (C/C++)作者 (woody)時間9年前 (2017/02/11 01:34), 9年前編輯推噓3(306)
留言9則, 3人參與, 最新討論串1/1
開發平台(Platform): (Ex: Win10, Linux, ...) Win 8 編譯器(Ex: GCC, clang, VC++...)+目標環境(跟開發平台不同的話需列出) LeetCode 最近在練習用C寫LeetCode的題目 題目: https://leetcode.com/problems/find-mode-in-binary-search-tree/ 我的想法是利用In-Order把BST的節點通通由小到大放入ary陣列中 再另外開一個陣列b來放每個元素出現的個數 程式碼: http://codepad.org/fRoufhCU (只附上leetcode需要的部分,main就沒貼上來) 假設BST的所有節點為{-3,1,1,2,2,3,4,4,5} (已由小到大排列),如下圖 http://imgur.com/GfhLdN5.jpg
在DevC上可以順利執行 但丟到LeetCode上就會有如右圖的錯誤 http://i.imgur.com/jeYB4ld.jpg
完全找不出錯在哪.......... 先謝謝各位了 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.64.211.188 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1486748062.A.07B.html

02/11 07:04, , 1F
*returnSize. Why is there a star?
02/11 07:04, 1F
因為LeetCode題目規定Return an array of size *returnSize int* findMode(struct TreeNode* root, int* returnSize) { } 也很明顯是要用傳記憶體位址的方式傳入 findMode function內 ※ 編輯: woody3724 (61.64.210.30), 02/11/2017 08:07:47

02/11 09:07, , 2F
你沒有處理到node->val是NULL的情況
02/11 09:07, 2F

02/11 09:34, , 3F
我猜你的code會把NULL當成一個值,所以[1,null,2,2]長是
02/11 09:34, 3F

02/11 09:34, , 4F
4 或許你在你的DevC上可以跑 但是assign一個int為NULL
02/11 09:34, 4F

02/11 09:35, , 5F
還蠻奇怪的 也不能保證每次都成功吧
02/11 09:35, 5F

02/11 10:14, , 6F
不好意思 請忽略我上面四行吧 XDDD
02/11 10:14, 6F

02/11 10:23, , 7F
本來以為要自己建樹所以才有上面的推文
02/11 10:23, 7F

02/11 10:24, , 8F
應該是錯在void inorder(...) 裡的static int index=0
02/11 10:24, 8F

02/11 10:25, , 9F
leetcode在換另一棵樹去跑你的index值還在,不會變回0
02/11 10:25, 9F
太感謝了!! 原來問題出在這!! 已經解決 感恩 ※ 編輯: woody3724 (61.64.210.30), 02/11/2017 10:51:51
文章代碼(AID): #1OdVcU1x (C_and_CPP)
文章代碼(AID): #1OdVcU1x (C_and_CPP)