keep moving

Binary Tree Inorder Traversal

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?

public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> list = new ArrayList<Integer>();
getInorderTraversal(root, list);
return list;
}

public void getInorderTraversal(TreeNode root,List<Integer> list){
if(root==null)
return;
getInorderTraversal(root.left, list);
getInorderTraversal(root.right, list);
}

public List<Integer> inorderTraversal(TreeNode root) {
Set<TreeNode> visited=new HashSet<TreeNode>();
List<Integer> result=new ArrayList<Integer>();
Stack<TreeNode> stack=new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()){
TreeNode p=stack.peek();
if(p.left!=null&&!visited.contains(p.left)){
stack.push(p.left);
}else{
p=stack.pop();
if(p.right!=null){
}
}
}
return result;
}

Binary Tree Inorder Traversal -- LeetCode

2014-03-01 05:38:48

【LeetCode】94. Binary Tree Inorder Traversal 解题报告

2016-05-22 23:59:26

【LeetCode】【Python】Binary Tree Inorder Traversal

2014-07-25 09:34:45

《leetCode》：Binary Tree Inorder Traversal

2016-01-03 19:47:08

LeetCode 94：Binary Tree Inorder Traversal（中序遍历）

2015-06-01 19:06:05

leetcode题解-94. Binary Tree Inorder Traversal

2017-03-05 22:07:29

leetcode-94:Binary Tree Inorder Traversal (Java)

2015-08-18 15:03:00

【LeetCode】105. Construct Binary Tree from Preorder and Inorder Traversal 解题报告

2016-06-01 23:42:08

LeetCode 94 Binary Tree Inorder Traversal(Python详解及实现)

2017-08-10 16:10:57

106 Construct Binary Tree from Inorder and Postorder Traversal

2015-12-09 20:29:35