Leetcode 94 Binary Tree Inorder Traversal

Leetcode 94 Binary Tree Inorder Traversal

//Leetcode 94  Binary Tree Inorder Traversal
#include "header.h"
#include 
struct TreeNode {
     int val;
     TreeNode *left;
     TreeNode *right;    
     TreeNode(int x) : val(x), left(NULL), right(NULL) {} 
};

class Solution {
public:
    vector<int> inorderTraversal(TreeNode* root) {
        vector<int> tranversal;
    stack tree;
    TreeNode* node = root;
    if(root == nullptr)
      return tranversal;

    while(!tree.empty()||node != nullptr) {
      while (node != nullptr) {
        tree.push(node);

        node = node->left;
     }
      if(node == nullptr) {//其实这个if没有用啊
        node = tree.top();
        tranversal.push_back(node->val);
        tree.pop();
        node = node->right;
      }
    }
    return tranversal;
    }
};

你可能感兴趣的:(Leetcode 94 Binary Tree Inorder Traversal)