Given a binary tree, return the inorder traversal of its nodes ' values.
For example:
Given binary Tree [1,null,2,3]
,
1 2 / 3
Return [1,3,2]
.
Note:recursive solution is trivial, could do it iteratively?
My AC code, recursive notation is relatively simple, so try to iterate:
Class Solution {public: vector<int> inordertraversal (treenode* root) { vector<int> nums; Stack<treenode *> Stacknode; while (root) { stacknode.push (root); if (root->left! = NULL) { root = root->left; Continue; } Nums.push_back (root->val); Stacknode.pop (); if (root->right! = NULL) { root = root->right; Continue; } TreeNode * node = root; if (!stacknode.empty ()) { root = Stacknode.top (); Root->left = NULL; Stacknode.pop (); } if (node = = root) break ; } return nums;} ;
Using the idea of recursive stack, I maintain a stack, the time complexity should be O (n), Space complexity O (n).
Look at the details after AC:
Run time is 3ms.
But I also wrote a recursive version of:
Class Solution {public: void helper (treenode* root, vector<int> & nums) { if (root) { Helper (root- >left, nums); Nums.push_back (root->val); Helper (Root->right, nums); } } Vector<int> inordertraversal (treenode* root) { vector<int> nums; Helper (root, nums); return nums;} ;
The result feels similar:
A complete iterative algorithm:
#include <iostream> #include <vector> #include <stack>using namespace Std;class Tree {public:int val; Tree * LEFT; Tree * RIGHT; Tree (): Val (), Left (null), right (null) {}void createtree (Tree * &root); tree* Insert (tree * root, int x), void Buildtree (tree* root, vector<int> nums), void inordertraversal (tree * root); voi D Iterative_inordertraversal (Tree * root);}; void Tree::createtree (Tree * &root) {int val; Cin >> Val; if (val = =-1) root = NULL; else {root = new Tree; Root->val = val; Createtree (Root->left); Createtree (Root->right); }}tree* Tree::insert (tree * root, int x) {Tree * node = new Tree;node->val = x;if (root = NULL) {root = node ; return root; }else if (root->left = = NULL) {root->left = node; return root; }else if (root->right = = NULL) {root->right = node; return root; } Else Node->left = Root;rEturn node;} void Tree::buildtree (tree* root, vector<int> nums) {for (auto n:nums) root = insert (root, n);} void Tree::inordertraversal (Tree * root) {if (root! = NULL) {inordertraversal (root->left); cout << Root->val & lt;< "; inordertraversal (Root->right);}} void Tree::iterative_inordertraversal (Tree * root) {stack<tree *> stacknode; while (root) {Stacknode.push (root); if (root->left! = NULL) {root = root->left; Continue } cout << root->val << "; Stacknode.pop (); if (root->right! = NULL) {root = root->right; Continue } Tree * node = root; if (!stacknode.empty ()) {root = Stacknode.top (); Root->left = NULL; Stacknode.pop (); } if (node = = root) break; }}int Main () {//vector<int> nums{; Tree * Root = Null;root->createtrEE (root);//root->buildtree (root, Nums); root->inordertraversal (root); cout << Endl; Root->iterative_inordertraversal (root); return 0;}
c++-iterative traversal of binary trees (leetcode.94-binary tree inorder Traversal)