1. 求二叉树中的节点个数
2. 求二叉树的深度
3. 前序遍历,中序遍历,后序遍历
4.分层遍历二叉树(按层次从上往下,从左往右)
5. 将二叉查找树变为有序的双向链表
6. 求二叉树第K层的节点个数
7. 求二叉树中叶子节点的个数
8. 判断两棵二叉树是否结构相同
9. 判断二叉树是否是平衡二叉树
10. 求二叉树的镜像
11. 求二叉树中两个节点的最低公共祖先节点
12. 求二叉树中节点的最大距离
13. 由前序遍历序列和中序遍历序列重建二叉树
14.判断二叉树是否是彻底二叉树java
二叉树节点定义以下:
struct BinaryTreeNode
{
int m_nValue;
BinaryTreeNode* m_pLeft;
BinaryTreeNode* m_pRight;
};
node
1. 求二叉树中的节点个数
递归解法:
(1)若是二叉树为空,节点个数为0
(2)若是二叉树不为空,二叉树节点个数 = 左子树节点个数 + 右子树节点个数 + 1linux
参考代码以下:算法
int GetNodeNum(BinaryTreeNode * pRoot) { if(pRoot == NULL) // 递归出口 return 0; return GetNodeNum(pRoot->m_pLeft) + GetNodeNum(pRoot->m_pRight) + 1; }
2. 求二叉树的深度ui
递归解法:this
(1)若是二叉树为空,二叉树的深度为0spa
(2)若是二叉树不为空,二叉树的深度 = max(左子树深度, 右子树深度) + 1指针
参考代码以下:code
int GetDepth(BinaryTreeNode * pRoot) { if(pRoot == NULL) // 递归出口 return 0; int depthLeft = GetDepth(pRoot->m_pLeft); int depthRight = GetDepth(pRoot->m_pRight); return depthLeft > depthRight ? (depthLeft + 1) : (depthRight + 1); }
3. 前序遍历,中序遍历,后序遍历orm
前序遍历递归解法:
(1)若是二叉树为空,空操做
(2)若是二叉树不为空,访问根节点,前序遍历左子树,前序遍历右子树
参考代码以下:
void PreOrderTraverse(BinaryTreeNode * pRoot) { if(pRoot == NULL) return; Visit(pRoot); // 访问根节点 PreOrderTraverse(pRoot->m_pLeft); // 前序遍历左子树 PreOrderTraverse(pRoot->m_pRight); // 前序遍历右子树 }
中序遍历递归解法
(1)若是二叉树为空,空操做。
(2)若是二叉树不为空,中序遍历左子树,访问根节点,中序遍历右子树
参考代码以下:
void InOrderTraverse(BinaryTreeNode * pRoot) { if(pRoot == NULL) return; InOrderTraverse(pRoot->m_pLeft); // 中序遍历左子树 Visit(pRoot); // 访问根节点 InOrderTraverse(pRoot->m_pRight); // 中序遍历右子树 }
后序遍历递归解法
(1)若是二叉树为空,空操做
(2)若是二叉树不为空,后序遍历左子树,后序遍历右子树,访问根节点
参考代码以下:
void PostOrderTraverse(BinaryTreeNode * pRoot) { if(pRoot == NULL) return; PostOrderTraverse(pRoot->m_pLeft); // 后序遍历左子树 PostOrderTraverse(pRoot->m_pRight); // 后序遍历右子树 Visit(pRoot); // 访问根节点 }
4.分层遍历二叉树(按层次从上往下,从左往右)
至关于广度优先搜索,使用队列实现。队列初始化,将根节点压入队列。当队列不为空,进行以下操做:弹出一个节点,访问,若左子节点或右子节点不为空,将其压入队列。
void LevelTraverse(BinaryTreeNode * pRoot) { if(pRoot == NULL) return; queue<BinaryTreeNode *> q; q.push(pRoot); while(!q.empty()) { BinaryTreeNode * pNode = q.front(); q.pop(); Visit(pNode); // 访问节点 if(pNode->m_pLeft != NULL) q.push(pNode->m_pLeft); if(pNode->m_pRight != NULL) q.push(pNode->m_pRight); } return; }
5. 将二叉查找树变为有序的双向链表
要求不能建立新节点,只调整指针。
递归解法:
(1)若是二叉树查找树为空,不须要转换,对应双向链表的第一个节点是NULL,最后一个节点是NULL
(2)若是二叉查找树不为空:
若是左子树为空,对应双向有序链表的第一个节点是根节点,左边不须要其余操做;
若是左子树不为空,转换左子树,二叉查找树对应双向有序链表的第一个节点就是左子树转换后双向有序链表的第一个节点,同时将根节点和左子树转换后的双向有序链 表的最后一个节点链接;
若是右子树为空,对应双向有序链表的最后一个节点是根节点,右边不须要其余操做;
若是右子树不为空,对应双向有序链表的最后一个节点就是右子树转换后双向有序链表的最后一个节点,同时将根节点和右子树转换后的双向有序链表的第一个节点链接。
参考代码以下:
/******************************************************************************
参数:
pRoot: 二叉查找树根节点指针
pFirstNode: 转换后双向有序链表的第一个节点指针
pLastNode: 转换后双向有序链表的最后一个节点指针
******************************************************************************/
void Convert(BinaryTreeNode * pRoot, BinaryTreeNode * & pFirstNode, BinaryTreeNode * & pLastNode){ BinaryTreeNode *pFirstLeft, *pLastLeft, * pFirstRight, *pLastRight; if(pRoot == NULL) { pFirstNode = NULL; pLastNode = NULL; return; } if(pRoot->m_pLeft == NULL) { // 若是左子树为空,对应双向有序链表的第一个节点是根节点 pFirstNode = pRoot; } else { Convert(pRoot->m_pLeft, pFirstLeft, pLastLeft); // 二叉查找树对应双向有序链表的第一个节点就是左子树转换后双向有序链表的第一个节点 pFirstNode = pFirstLeft; // 将根节点和左子树转换后的双向有序链表的最后一个节点链接 pRoot->m_pLeft = pLastLeft; pLastLeft->m_pRight = pRoot; } if(pRoot->m_pRight == NULL) { // 对应双向有序链表的最后一个节点是根节点 pLastNode = pRoot; } else { Convert(pRoot->m_pRight, pFirstRight, pLastRight); // 对应双向有序链表的最后一个节点就是右子树转换后双向有序链表的最后一个节点 pLastNode = pLastRight; // 将根节点和右子树转换后的双向有序链表的第一个节点链接 pRoot->m_pRight = pFirstRight; p FirstRight->m_pLeft = pRoot; } return; }
6. 求二叉树第K层的节点个数
递归解法:
(1)若是二叉树为空或者k<1返回0
(2)若是二叉树不为空而且k==1,返回1
(3)若是二叉树不为空且k>1,返回左子树中k-1层的节点个数与右子树k-1层节点个数之和
参考代码以下:
int GetNodeNumKthLevel(BinaryTreeNode * pRoot, int k) { if(pRoot == NULL || k < 1) return 0; if(k == 1) return 1; int numLeft = GetNodeNumKthLevel(pRoot->m_pLeft, k-1); // 左子树中k-1层的节点个数 int numRight = GetNodeNumKthLevel(pRoot->m_pRight, k-1); // 右子树中k-1层的节点个数 return (numLeft + numRight); }
7. 求二叉树中叶子节点的个数
递归解法:
(1)若是二叉树为空,返回0
(2)若是二叉树不为空且左右子树为空,返回1
(3)若是二叉树不为空,且左右子树不一样时为空,返回左子树中叶子节点个数加上右子树中叶子节点个数
参考代码以下:
int GetLeafNodeNum(BinaryTreeNode * pRoot) { if(pRoot == NULL) return 0; if(pRoot->m_pLeft == NULL && pRoot->m_pRight == NULL) return 1; int numLeft = GetLeafNodeNum(pRoot->m_pLeft); // 左子树中叶节点的个数 int numRight = GetLeafNodeNum(pRoot->m_pRight); // 右子树中叶节点的个数 return (numLeft + numRight); }
8. 判断两棵二叉树是否结构相同
不考虑数据内容。结构相赞成味着对应的左子树和对应的右子树都结构相同。
递归解法:
(1)若是两棵二叉树都为空,返回真
(2)若是两棵二叉树一棵为空,另外一棵不为空,返回假
(3)若是两棵二叉树都不为空,若是对应的左子树和右子树都同构返回真,其余返回假
参考代码以下:
bool StructureCmp(BinaryTreeNode * pRoot1, BinaryTreeNode * pRoot2) { if(pRoot1 == NULL && pRoot2 == NULL) // 都为空,返回真 return true; else if(pRoot1 == NULL || pRoot2 != NULL) // 有一个为空,一个不为空,返回假 return false; bool resultLeft = StructureCmp(pRoot1->m_pLeft, pRoot2->m_pLeft); // 比较对应左子树 bool resultRight = StructureCmp(pRoot1->m_pRight, pRoot2->m_pRight); // 比较对应右子树 return (resultLeft && resultRight); }
9. 判断二叉树是否是平衡二叉树
递归解法:
(1)若是二叉树为空,返回真
(2)若是二叉树不为空,若是左子树和右子树都是AVL树而且左子树和右子树高度相差不大于1,返回真,其余返回假
参考代码:
bool IsAVL(BinaryTreeNode * pRoot, int & height) { if(pRoot == NULL) // 空树,返回真 { height = 0; return true; } int heightLeft; bool resultLeft = IsAVL(pRoot->m_pLeft, heightLeft); int heightRight; bool resultRight = IsAVL(pRoot->m_pRight, heightRight); if(resultLeft && resultRight && abs(heightLeft - heightRight) <= 1) // 左子树和右子树都是AVL,而且高度相差不大于1,返回真 { height = max(heightLeft, heightRight) + 1; return true; } else { height = max(heightLeft, heightRight) + 1; return false; } }
10. 求二叉树的镜像
递归解法:
(1)若是二叉树为空,返回空
(2)若是二叉树不为空,求左子树和右子树的镜像,而后交换左子树和右子树
参考代码以下:
BinaryTreeNode * Mirror(BinaryTreeNode * pRoot) { if(pRoot == NULL) // 返回NULL return NULL; BinaryTreeNode * pLeft = Mirror(pRoot->m_pLeft); // 求左子树镜像 BinaryTreeNode * pRight = Mirror(pRoot->m_pRight); // 求右子树镜像 // 交换左子树和右子树 pRoot->m_pLeft = pRight; pRoot->m_pRight = pLeft; return pRoot; }
11. 求二叉树中两个节点的最低公共祖先节点
递归解法:
(1)若是两个节点分别在根节点的左子树和右子树,则返回根节点
(2)若是两个节点都在左子树,则递归处理左子树;若是两个节点都在右子树,则递归处理右子树
参考代码以下:
bool FindNode(BinaryTreeNode * pRoot, BinaryTreeNode * pNode) { if(pRoot == NULL || pNode == NULL) return false; if(pRoot == pNode) return true; bool found = FindNode(pRoot->m_pLeft, pNode); if(!found) found = FindNode(pRoot->m_pRight, pNode); return found; }
BinaryTreeNode * GetLastCommonParent(BinaryTreeNode * pRoot, BinaryTreeNode * pNode1, BinaryTreeNode * pNode2) { if(FindNode(pRoot->m_pLeft, pNode1)) { if(FindNode(pRoot->m_pRight, pNode2)) return pRoot; else return GetLastCommonParent(pRoot->m_pLeft, pNode1, pNode2); } else { if(FindNode(pRoot->m_pLeft, pNode2)) return pRoot; else return GetLastCommonParent(pRoot->m_pRight, pNode1, pNode2); } }
递归解法效率很低,有不少重复的遍历,下面看一下非递归解法。
非递归解法:
先求从根节点到两个节点的路径,而后再比较对应路径的节点就行,最后一个相同的节点也就是他们在二叉树中的最低公共祖先节点
参考代码以下:
bool GetNodePath(BinaryTreeNode * pRoot, BinaryTreeNode * pNode, list<BinaryTreeNode *> & path) { if(pRoot == pNode) { path.push_back(pRoot); return true; } if(pRoot == NULL) return false; path.push_back(pRoot); bool found = false; found = GetNodePath(pRoot->m_pLeft, pNode, path); if(!found) found = GetNodePath(pRoot->m_pRight, pNode, path); if(!found) path.pop_back(); return found; } BinaryTreeNode * GetLastCommonParent(BinaryTreeNode * pRoot, BinaryTreeNode * pNode1, BinaryTreeNode * pNode2) { if(pRoot == NULL || pNode1 == NULL || pNode2 == NULL) return NULL; list<BinaryTreeNode*> path1; bool bResult1 = GetNodePath(pRoot, pNode1, path1); list<BinaryTreeNode*> path2; bool bResult2 = GetNodePath(pRoot, pNode2, path2); if(!bResult1 || !bResult2) return NULL; BinaryTreeNode * pLast = NULL; list<BinaryTreeNode*>::const_iterator iter1 = path1.begin(); list<BinaryTreeNode*>::const_iterator iter2 = path2.begin(); while(iter1 != path1.end() && iter2 != path2.end()) { if(*iter1 == *iter2) pLast = *iter1; else break; iter1++; iter2++; } return pLast; }
在上述算法的基础上稍加变化便可求二叉树中任意两个节点的距离了。
12. 求二叉树中节点的最大距离
即二叉树中相距最远的两个节点之间的距离。
递归解法:
(1)若是二叉树为空,返回0,同时记录左子树和右子树的深度,都为0
(2)若是二叉树不为空,最大距离要么是左子树中的最大距离,要么是右子树中的最大距离,要么是左子树节点中到根节点的最大距离+右子树节点中到根节点的最大距离,同时记录左子树和右子树节点中到根节点的最大距离。
参考代码以下:
int GetMaxDistance(BinaryTreeNode * pRoot, int & maxLeft, int & maxRight) { // maxLeft, 左子树中的节点距离根节点的最远距离 // maxRight, 右子树中的节点距离根节点的最远距离 if(pRoot == NULL) { maxLeft = 0; maxRight = 0; return 0; } int maxLL, maxLR, maxRL, maxRR; int maxDistLeft, maxDistRight; if(pRoot->m_pLeft != NULL) { maxDistLeft = GetMaxDistance(pRoot->m_pLeft, maxLL, maxLR); maxLeft = max(maxLL, maxLR) + 1; } else { maxDistLeft = 0; maxLeft = 0; } if(pRoot->m_pRight != NULL) { maxDistRight = GetMaxDistance(pRoot->m_pRight, maxRL, maxRR); maxRight = max(maxRL, maxRR) + 1; } else { maxDistRight = 0; maxRight = 0; } return max(max(maxDistLeft, maxDistRight), maxLeft+maxRight); }
13. 由前序遍历序列和中序遍历序列重建二叉树
二叉树前序遍历序列中,第一个元素老是树的根节点的值。中序遍历序列中,左子树的节点的值位于根节点的值的左边,右子树的节点的值位
于根节点的值的右边。
递归解法:
(1)若是前序遍历为空或中序遍历为空或节点个数小于等于0,返回NULL。
(2)建立根节点。前序遍历的第一个数据就是根节点的数据,在中序遍历中找到根节点的位置,可分别得知左子树和右子树的前序和中序遍
历序列,重建左右子树。
BinaryTreeNode * RebuildBinaryTree(int* pPreOrder, int* pInOrder, int nodeNum) { if(pPreOrder == NULL || pInOrder == NULL || nodeNum <= 0) return NULL; BinaryTreeNode * pRoot = new BinaryTreeNode; // 前序遍历的第一个数据就是根节点数据 pRoot->m_nValue = pPreOrder[0]; pRoot->m_pLeft = NULL; pRoot->m_pRight = NULL; // 查找根节点在中序遍历中的位置,中序遍历中,根节点左边为左子树,右边为右子树 int rootPositionInOrder = -1; for(int i = 0; i < nodeNum; i++) if(pInOrder[i] == pRoot->m_nValue) { rootPositionInOrder = i; break; } if(rootPositionInOrder == -1) { throw std::exception("Invalid input."); } // 重建左子树 int nodeNumLeft = rootPositionInOrder; int * pPreOrderLeft = pPreOrder + 1; int * pInOrderLeft = pInOrder; pRoot->m_pLeft = RebuildBinaryTree(pPreOrderLeft, pInOrderLeft, nodeNumLeft); // 重建右子树 int nodeNumRight = nodeNum - nodeNumLeft - 1; int * pPreOrderRight = pPreOrder + 1 + nodeNumLeft; int * pInOrderRight = pInOrder + nodeNumLeft + 1; pRoot->m_pRight = RebuildBinaryTree(pPreOrderRight, pInOrderRight, nodeNumRight); return pRoot; }
一样,有中序遍历序列和后序遍历序列,相似的方法可重建二叉树,但前序遍历序列和后序遍历序列不一样恢复一棵二叉树,证实略。
14.判断二叉树是否是彻底二叉树
若设二叉树的深度为h,除第 h 层外,其它各层 (1~h-1) 的结点数都达到最大个数,第 h 层全部的结点都连续集中在最左边,这就是彻底二叉树。
有以下算法,按层次(从上到下,从左到右)遍历二叉树,当遇到一个节点的左子树为空时,则该节点右子树必须为空,且后面遍历的节点左右子树都必须为空,不然不是彻底二叉树。
bool IsCompleteBinaryTree(BinaryTreeNode * pRoot) { if(pRoot == NULL) return false; queue<BinaryTreeNode *> q; q.push(pRoot); bool mustHaveNoChild = false; bool result = true; while(!q.empty()) { BinaryTreeNode * pNode = q.front(); q.pop(); if(mustHaveNoChild) // 已经出现了有空子树的节点了,后面出现的必须为叶节点(左右子树都为空) { if(pNode->m_pLeft != NULL || pNode->m_pRight != NULL) { result = false; break; } } else { if(pNode->m_pLeft != NULL && pNode->m_pRight != NULL) { q.push(pNode->m_pLeft); q.push(pNode->m_pRight); } else if(pNode->m_pLeft != NULL && pNode->m_pRight == NULL) { mustHaveNoChild = true; q.push(pNode->m_pLeft); } else if(pNode->m_pLeft == NULL && pNode->m_pRight != NULL) { result = false; break; } else { mustHaveNoChild = true; } } } return result; }
=======================================
import java.util.ArrayList; import java.util.List; public class MaxLenInBinTree { /* a. 1 / \ 2 3 / \ / \ 4 5 6 7 max=4 pass "root" b. 1 / \ 2 3 / \ 4 5 / \ 6 7 / \ 8 9 max=6. do not pass "root" */ public static void main(String[] args) { int[] a={1,2,3,4,5,6,7}; //store in LevelOrder,Complete Binary Tree. 0==no child int[] b={1,2,3,4,5,0,0,6,0,0,7,0,0,0,0,8,0,0,0,0,0,0,9}; MaxLenInBinTree m=new MaxLenInBinTree(); Node aRoot=m.createTree(b); m.findMaxLen(aRoot); System.out.println(m.maxLen); } private int maxLen=0; public void findMaxLen(Node node){ if(node==null) return ; if(node.getLeft()==null){ node.setMaxLeftLen(0); } if(node.getRight()==null){ node.setMaxRightLen(0); } if(node.getLeft()!=null){ findMaxLen(node.getLeft()); } if(node.getRight()!=null){ findMaxLen(node.getRight()); } if(node.getLeft()!=null){ int temp=0; Node left=node.getLeft(); if(left.getMaxLeftLen()>left.getMaxRightLen()){ temp=left.getMaxLeftLen(); }else{ temp=left.getMaxRightLen(); } node.setMaxLeftLen(temp+1); } if(node.getRight()!=null){ int temp=0; Node right=node.getRight(); if(right.getMaxLeftLen()>right.getMaxRightLen()){ temp=right.getMaxLeftLen(); }else{ temp=right.getMaxRightLen(); } node.setMaxRightLen(temp+1); } if(maxLen<node.getMaxLeftLen()+node.getMaxRightLen()){ maxLen=node.getMaxLeftLen()+node.getMaxRightLen(); } } public Node createTree(int[] data){ List<Node> nodeList=new ArrayList<Node>(); for(int each:data){ Node n=new Node(each); nodeList.add(n); } int lastRootIndex=data.length/2-1; for(int i=0;i<=lastRootIndex;i++){ Node root=nodeList.get(i); Node left=nodeList.get(i*2+1); if(left.getData()!=0){ root.setLeft(left); }else{ root.setLeft(null); } if(i*2+2<data.length){ Node right=nodeList.get(i*2+2); if(right.getData()!=0){ root.setRight(right); }else{ root.setRight(null); } } } Node root=nodeList.get(0); return root; } class Node{ private int data; private Node left; private Node right; private int maxLeftLen;//the max length of leftTree private int maxRightLen; public Node(int i){ data=i; } public int getData() { return data; } public void setData(int data) { this.data = data; this.left=null; this.right=null; } public Node getLeft() { return left; } public void setLeft(Node left) { this.left = left; } public Node getRight() { return right; } public void setRight(Node right) { this.right = right; } public int getMaxLeftLen() { return maxLeftLen; } public void setMaxLeftLen(int maxLeftLen) { this.maxLeftLen = maxLeftLen; } public int getMaxRightLen() { return maxRightLen; } public void setMaxRightLen(int maxRightLen) { this.maxRightLen = maxRightLen; } } }