Jude whether a binary tree is well ordered

View previous topic View next topic Go down

Jude whether a binary tree is well ordered

Post  Admin on Wed Nov 02, 2011 10:19 pm

public boolean judge(TreeNode root){
Stack<TreeNode> stack=new Stack<TreeNode>();
ArrayList<TreeNode> arr=new ArrayList<TreeNode>();
pushLeft(root,stack);
while(!stack.isEmpty()){
TreeNode temp=stack.pop();
arr.add(temp);
pushLeft(temp.right,stack);
}
for(int i=0;i<arr.size()-1;i++){
if(arr.getIndex(i)>arr.getIndex(i+1)){
return false;
}
}
return true;
}

public void pushLeft(TreeNode node, Stack s){
while(node!=null){
s.push(node);
node=node.left;
}
}

Admin
Admin

Posts : 131
Join date : 2011-08-16

View user profile http://codefornongeek.forumotion.com

Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum