19.2.24 [LeetCode 94] Binary Tree Inorder Traversal
发布时间:2020-12-14 04:23:21 所属栏目:大数据 来源:网络整理
导读:Given a binary tree,return the? inorder ?traversal of its nodes‘ values. Example: Input: [1,null,2,3] 1 2 / 3Output: [1,3,2] 1 class Solution { 2 public : 3 void build(TreeNode*root,vector int ans) { 4 if (root == NULL) return ; 5 build(r
Given a binary tree,return the?inorder?traversal of its nodes‘ values. Example: Input: [1,null,2,3]
1
2
/
3
Output: [1,3,2]
1 class Solution { 2 public: 3 void build(TreeNode*root,vector<int>&ans) { 4 if (root == NULL)return; 5 build(root->left,ans); 6 ans.push_back(root->val); 7 build(root->right,ans); 8 } 9 vector<int> inorderTraversal(TreeNode* root) { 10 vector<int>ans; 11 build(root,ans); 12 return ans; 13 } 14 }; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |