Code For NonGeek
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Jude whether a binary tree is well ordered

Go down

Jude whether a binary tree is well ordered Empty Jude whether a binary tree is well ordered

Post  Admin 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

https://codefornongeek.forumotion.com

Back to top Go down

Back to top

- Similar topics

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