Binary Tree Preorder Traversal!
参考【python实现二叉树】用递归写了下面的代码
[javascript]var result = [];var preorderTraversal = function(root) { if(root){ result.push(root.val); preorderTraversal(root.left); preorderTraversal(root.right); } return result; }; [python]class Solution(object): result = [] def preorderTraversal(self, root): if root: self.result.append(root.val) self.preorderTraversal(root.left) self.preorderTraversal(root.right) # print "self.result=", self.result return self.result [cpp]class Solution {public: vector<int>res; vector<int> preorderTraversal(TreeNode *root) { if(root == NULL) return res; res.push_back(root->val); res = preorderTraversal(root->left); res = preorderTraversal(root->right); return res; } };
用 python 刷题时,出现错误,但改写成 cpp 后,就通过了...
其实,想问的是:python 与 javascrip 错了?错哪了?
qq_遁去的一_1
相关分类