LeetCode:Binary Tree Inorder Traversal

来源:互联网 发布:淘宝刷流量会被降权 编辑:程序博客网 时间:2024/06/14 10:13

Given a binary tree, return the inorder traversal of its nodes' values.

For example:
Given binary tree {1,#,2,3},

   1    \     2    /   3

return [1,3,2].

Note: Recursive solution is trivial, could you do it iteratively?

// Source : https://oj.leetcode.com/problems/binary-tree-inorder-traversal/  // Author : Chao Zeng  // Date   : 2014-12-19 struct TreeNode {int val;TreeNode *left;TreeNode *right;TreeNode(int x) : val(x),left(NULL),right(NULL){}};//递归方法class Solution {public:vector<int> path;void inorder(TreeNode *root){if (!root)return;inorder(root->left);path.push_back(root->val);inorder(root->right);}vector<int> inorderTraversal(TreeNode *root) {inorder(root);return path;}};


0 0