Jude whether a binary tree is well ordered
Page 1 of 1
Jude whether a binary tree is well ordered
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;
}
}
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;
}
}
Similar topics
» Write a program to determine if a binary tree is well ordered
» To get mirror image of a binary tree
» You are given a TreeNode, and you have to write an algorithm which will return the no. of nodes in the tree.
» Design an algorithm and write code to serialize and deserialize a binary tree/graph
» Judege whether a number's binary expression is palidrom
» To get mirror image of a binary tree
» You are given a TreeNode, and you have to write an algorithm which will return the no. of nodes in the tree.
» Design an algorithm and write code to serialize and deserialize a binary tree/graph
» Judege whether a number's binary expression is palidrom
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|