/** 94. Binary Tree inorder Traversal * 2015.11.21 by Mingyang * With the same DFS as preorder, but every time pop comes out in the small books, that is, in the list inside the sequence traversal iterative solution * Use the stack to add all the left children of the root node to the stack, then output the top element of the stack, and then handle the right subtree of the top element of the stack .*/ PublicList<integer>inordertraversal (TreeNode root) {List<Integer> list =NewArraylist<integer>(); if(Root = =NULL) returnlist; Stack<TreeNode> stack =NewStack<treenode>(); TreeNode P=Root; while(!stack.isempty () | | P! =NULL) { if(P! =NULL) {Stack.push (P); P=P.left; } Else{TreeNode T=Stack.pop (); List.add (T.val); P=T.right; } } returnlist; } PublicList<integer>inorderTraversal1 (TreeNode root) {List<Integer> res =NewArraylist<integer>(); DFS (root, res); returnRes; } Public voidDFS (TreeNode root, list<integer>Res) { if(Root = =NULL) return; DFS (Root.left, res); Res.add (Root.val); DFS (Root.right, res); }
94. Binary Tree Inorder Traversal