leetcode94. 二叉树的中序遍历(左中右)
生活随笔
收集整理的這篇文章主要介紹了
leetcode94. 二叉树的中序遍历(左中右)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
二:上碼
/*** Definition for a binary tree node.* struct TreeNode {* int val;* TreeNode *left;* TreeNode *right;* TreeNode() : val(0), left(nullptr), right(nullptr) {}* TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}* TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}* };*/ class Solution { public:/**遞歸三部曲:1.確定遞歸函數的參數和返回值2.確定遞歸終止條件3.確定遞歸體 */void order(TreeNode* root,vector<int>&v) {if(root == NULL) return;order(root->left,v);v.push_back(root->val);order(root->right,v);} vector<int> inorderTraversal(TreeNode* root) {vector<int> ans;order(root,ans);return ans;} };總結
以上是生活随笔為你收集整理的leetcode94. 二叉树的中序遍历(左中右)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据恢复软件真的能恢复吗数据恢复软件是真
- 下一篇: 华为手机四个实用小技巧华为手机使用技巧和