LeetCode 94. 二叉树的中序遍历

题目链接:https://leetcode-cn.com/problems/binary-tree-inorder-traversal/

给定一个二叉树,返回它的中序 遍历。

 1 /**
 2  * Definition for a binary tree node.
 3  * struct TreeNode {
 4  *     int val;
 5  *     TreeNode *left;
 6  *     TreeNode *right;
 7  *     TreeNode(int x) : val(x), left(NULL), right(NULL) {}
 8  * };
 9  */
10 class Solution {
11 public:
12     vector<int> inorderTraversal(TreeNode* root) {
13         vector<int> result;
14         const TreeNode *p=root;
15         stack<const TreeNode *> s;
16         while(!s.empty()||p!=nullptr){
17             if(p!=nullptr){
18                 s.push(p);
19                 p=p->left;
20             }else{
21                 p=s.top();
22                 s.pop();
23                 result.push_back(p->val);
24                 p=p->right;
25             }
26         }
27         return result;
28     }
29 };
原文地址:https://www.cnblogs.com/wydxry/p/11836566.html