您的位置:首页 > 其它

leetcode~Binary Tree Inorder Traversal

2017-02-17 13:41 302 查看
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].

public class InorderTraversal {
public List<Integer> inorderTraversal(TreeNode root) {
List<Integer> result = new ArrayList<Integer>();
if(root==null) return result;
TreeNode node = root;
Stack<TreeNode> stack = new Stack<TreeNode>();
while(!stack.isEmpty() || node!=null) {
if(node!=null) {
stack.push(node);
node=node.left;
} else {
TreeNode tmp = stack.pop();
result.add(tmp.val);
node = tmp.right;
}
}
return result;
}

List<Integer> result = new ArrayList<Integer>();
public List<Integer> inorderTraversal2(TreeNode root) {
//递归实现
if(root!=<
4000
span class="hljs-built_in">null) {
helper(root);
}
return result;
}
public void helper(TreeNode p) {
if(p.left!=null) {
helper(p.left);
}
result.add(p.val);
if(p.right!=null) {
helper(p.right);
}
}

public List<Integer> inorderTraversal3(TreeNode root){
List<Integer> result = new ArrayList<Integer>();
if(root == null) {
return result;
}
Stack<TreeNode> stack =new Stack<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode top = stack.peek(); //不弹出栈,查看栈顶元素
if(top.left!=null) {
stack.push(top.left);
top.left=null;
} else {
result.add(top.val);
stack.pop();
if(top.right!=null){
stack.push(top.right);
}
}
}
return result;
}

//第一次访问节点时则展开,并且自己重新入栈,第二次从栈中访问则计入遍历
public List<Integer> inorderTraversal4(TreeNode root){
List<Integer> result = new ArrayList<Integer>();
Stack<TreeNode> stack = new Stack<TreeNode>();
if(root==null) return result;
//采用hashset判断是否访问过
HashSet<TreeNode> hs = new HashSet<TreeNode>();
stack.push(root);
while(!stack.isEmpty()) {
TreeNode tmp = stack.pop();
if(hs.contains(tmp)) {
result.add(tmp.val);
continue;
}
hs.add(tmp);
if(tmp.right!=null) {
stack.push(tmp.right);
}
stack.push(tmp);
if(tmp.left!=null) {
stack.push(tmp.left);
}
}
return result;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: