标签:
注意事项:
判断两棵二叉树是否相同(结构和值都相等)
判断二叉树是否为完全二叉树
判断二叉树是否为平衡二叉树
求二叉树的镜像
求二叉树中节点的最大距离
哈夫曼树的构建
将二叉查找树变为有序的双向链表
求二叉树中两个节点的最低公共祖先节点
输入两棵二叉树A和B,判断B是否为A的子树
//递归:前序遍历 public void preOrder(Node node){ if(node!=null){ System.out.print(node.getPerson().getKey()+"\t"); preOrder(node.getLeftChild()); preOrder(node.getRightChild()); } } //非递归:前序遍历 private void preOrder_2(){ Node curNode = rootNode; while(curNode!=null){ //打印当前节点 System.out.print(curNode.getPerson().getKey()+"\t"); //入栈 stack.push(curNode); //指向左子节点 curNode = curNode.getLeftChild(); //查找最左边的子节点 while(curNode == null&&!stack.isEmpty()){ curNode = stack.peek();//取栈顶元素 stack.pop();//出栈 curNode = curNode.getRightChild(); } } }
<span style="white-space:pre"> </span>//递归:中序遍历 public void midOrder(Node node){ if(node!=null){ midOrder(node.getLeftChild()); System.out.print(node.getPerson().getKey()+"\t"); midOrder(node.getRightChild()); } } //非递归:中序遍历(左根右) public void midOrder_2(){ Node curNode = rootNode; while(curNode!=null||!stack.isEmpty()){ if(curNode.getLeftChild()!=null){ stack.push(curNode); curNode = curNode.getLeftChild(); }else { //打印最左端的节点 System.out.print(curNode.getPerson().getKey()+"\t"); curNode = curNode.getRightChild();//指向右子节点 while(curNode == null&&!stack.isEmpty()){ curNode = stack.peek(); System.out.print(curNode.getPerson().getKey()+"\t"); stack.pop(); curNode = curNode.getRightChild(); } } } }后序遍历:
//递归:后序遍历 public void behOrder(Node node){ if (node!=null) { behOrder(node.getLeftChild()); behOrder(node.getRightChild()); System.out.print(node.getPerson().getKey()+"\t"); } } //非递归:后序遍历(左根右) public void behOrder_2(){ Node curNode = rootNode; Node preNode = null; //先将根入栈 stack.push(curNode); while(!stack.isEmpty()){ curNode = stack.peek();//当前节点设置为栈顶节点 if(curNode.getLeftChild()==null&&curNode.getRightChild()==null ||(preNode!=null&&(curNode.getLeftChild()==preNode||curNode.getRightChild()==preNode))){ //当前节点无左右节点,或者有左节点或右节点,但已经被访问过 //则直接输出该节点,将其出栈,将其设为上一个访问的节点 System.out.print(curNode.getPerson().getKey()+"\t"); stack.pop(); preNode = curNode;//已被访问过 }else { //如果不满足上面两种情况,则将其右孩子左孩子依次入栈(先右节点再左节点) if (curNode.getRightChild()!=null) { stack.push(curNode.getRightChild()); } if(curNode.getLeftChild()!=null){ stack.push(curNode.getLeftChild()); } } } }层次遍历:
<span style="white-space:pre"> </span>public int levelOrder(TreeNode root,int k) { Queue queue = new Queue(1024); List<List<Integer>> list = new ArrayList<List<Integer>>(); if(root==null){ return 0; } queue.push(root); //每层节点集 List<Integer> level = new ArrayList<Integer>(); //每层的节点个数 int number = 0; int levelCount = 0; do{ Queue queue2 = new Queue(1024); while(!queue.isEmpty()){ //取队首节点 TreeNode head = queue.peek(); level.add(head.val); number++;//结点个数加1 //弹出队头 queue.pop(); if(head.left!=null){ queue2.push(head.left); } if(head.right!=null){ queue2.push(head.right); } } levelCount++;//层级加1 if(levelCount==k){ return number; } list.add(level); level = new ArrayList<Integer>(); number = 0; queue = queue2; }while(!queue.isEmpty()); return number; } public class Queue { /** * 实现队列 */ int first,last,maxSize; TreeNode[] queue; public Queue(int size){ first = last = -1; maxSize = size; queue = new TreeNode[maxSize]; } public boolean isEmpty(){ if(first==-1){ return true; }else { return false; } } public boolean isFull(){ if(first==(last+1)%maxSize){ return true; }else { return false; } } //push public boolean push(TreeNode num){ if(this.isFull()){ System.out.println("queue is full!"); return false; }if(this.isEmpty()){ first = last = 0; }else{ last = (last+1)%maxSize; } queue[last] = num; return true; } public TreeNode pop(){ TreeNode num = queue[first]; if(this.isEmpty()){ queue = new TreeNode[maxSize]; return null; } if(first==last){ //到达队尾,清空数组 queue = new TreeNode[maxSize]; first = last = -1; return num; } first=(first+1)%maxSize; return num; } public TreeNode peek(){ if(first==-1) return null; else return queue[first]; } }
判断两棵二叉树是否相同(结构和值都相等)
判断二叉树是否为完全二叉树
判断二叉树是否为平衡二叉树
求二叉树的镜像
求二叉树中节点的最大距离
哈夫曼树的构建
将二叉查找树变为有序的双向链表
求二叉树中两个节点的最低公共祖先节点
输入两棵二叉树A和B,判断B是否为A的子树
标签:
原文地址:http://blog.csdn.net/u010796790/article/details/51926166