} res.push_back(root ->val);if(root ->right != NULL) {inorderTraversal(root ->right); } return res; } }; 迭代: classSolution{public:vector<int>inorderTraversal(TreeNode* root){ vector<int> res; stack<TreeNode*> s; TreeNode *cur = root;while(!s.empty() || cur !=NULL) {i...
https://leetcode.com/problems/binary-tree-inorder-traversal/ 题意 二叉树的中序遍历 代码(递归,C++) 其中函数返回类型不符合题目中要求,做相应更改即可A; /** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode *right; * TreeNode(int x) :...
left(NULL), right(NULL) {} * }; */ class Solution { public: vector<int> inorderTraversal(TreeNode* root) { vector<int> ret; if(nullptr == root) return ret; inOrder(root, ret); return ret; } private: void inOrder(TreeNode* root, vector<int>& ret) { ...
private TreeNode helper(int[] preorder, int preL, int preR, int[] inorder, int inL, int inR, HashMap<Integer, Integer> map) { if(preL>preR || inL>inR) return null; TreeNode root = new TreeNode(preorder[preL]); int index = map.get(root.val); root.left = helper(preorder, ...
Val) // 接下来该处理 cur 的右子树 cur = cur.Right } return ans } 题目链接: Binary Tree Inorder Traversal: leetcode.com/problems/b 二叉树的中序遍历: leetcode.cn/problems/bi LeetCode 日更第 238 天,感谢阅读至此的你 欢迎点赞、收藏鼓励支持小满...
LeetCode: 105. Construct Binary Tree from Preorder and Inorder Traversal 题目描述 Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that duplicates do not exist in the tree. For example, given ...
Given the root of a binary tree, return the inorder traversal of its nodes' values. Example 1: Input: root = [1,null,2,3] Output: [1,3,2] Explanation: Example 2: Input: root = [1,2,3,4,5,null,8,null,null,6,7,9] Output: [4,2,6,5,7,1,3,9,8] Explanation: Example...
名字叫做, morrois traversal, 自己写了下: My code: /** * Definition for a binary tree node. * public class TreeNode { * int val; * TreeNode left; * TreeNode right; * TreeNode(int x) { val = x; } * } */publicclassSolution{publicList<Integer>inorderTraversal(TreeNoderoot){List...
http://bangbingsyb.blogspot.com/2014/11/leetcode-construct-binary-tree-from.html ** 总结: 注意点已经在上面全部加粗表示了。 ** Anyway, Good luck, Richardo! My code: /** * Definition for a binary tree node. * public class TreeNode { ...
Binary Tree Inorder Traversal 二叉树的中序遍历 Given a binary tree, return the inorder traversal of its nodes' values. For example: Given binary tree {1,#,2,3}, return [1,3,2]. Note: Recursive solution is trivial, c...Leetcode 94.二叉树的中序遍历(Binary Tree Inorder Traversal) ...