Binary Tree Inorder Traversal

Binary Tree Inorder Traversal
 1 public class Solution {
 2     public ArrayList<Integer> inorderTraversal(TreeNode root) {
 3       ArrayList<Integer> res = new ArrayList<Integer> ();
 4       Stack<TreeNode> stack = new Stack<TreeNode>();
 5       TreeNode cur = root;
 6       while(!stack.isEmpty()||cur!=null){
 7           if(cur!=null){
 8               stack.push(cur);
 9               cur = cur.left;
10           }
11           else{
12               cur = stack.pop();
13               res.add(cur.val);
14               cur = cur.right;
15           }
16       }
17       return res;
18     }
19 }
View Code

Binary Tree Inorder Traversal

上一篇:Binary Tree Level Order Traversal


下一篇:Edit Distance