二叉树的基本操作
二叉树的遍历将其遍历到的结果存储到List中
代码:
前序遍历:
public List<TreeNode> preOrder2(TreeNode root) {List<TreeNode> ret=new ArrayList<>();if(root==null){return ret;}ret.add(root);List<TreeNode> leftTree=preOrder2(root.left);ret.addAll(leftTree);List<TreeNode> rightTree=preOrder2(root.right);ret.addAll(rightTree);return ret;}
中序遍历:
public List<TreeNode> inOrder2(TreeNode root) {List<TreeNode> ret=new ArrayList<>();if(root==null){return ret;}List<TreeNode> leftTree=inOrder2(root.left);ret.addAll(leftTree);ret.add(root);List<TreeNode> rightTree=inOrder2(root.right);ret.addAll(rightTree);return ret;}
后序遍历:
public List<TreeNode> postOrder2(TreeNode root) {List<TreeNode> ret=new ArrayList<>();if(root==null){return ret;}List<TreeNode> leftTree=postOrder2(root.left);ret.addAll(leftTree);List<TreeNode> rightTree=postOrder2(root.right);ret.addAll(rightTree);ret.add(root);return ret;}
二叉树的基本操作:
树中结点的个数
思路:以前中后序遍历这棵树的时候,会把每个节点都遍历到,遍历一次,我们就计数一次!
public int size=0;public int nodeSize(TreeNode root){if (root==null)return 0;size++;nodeSize(root.left);nodeSize(root.right);return size;}
利用子问题解决:
思路:
要算当前树总共有多少个节点,其实就是左子树的节点个数+右子树的节点个数+1.
public int nodeSize2(TreeNode root){if (root==null)return 0;return nodeSize2(root.left)+nodeSize2(root.right)+1;}
获取叶子节点的个数
思路:遍历二叉树,左右都为空时++。
public int leafSize=0;public void getLeafSize(TreeNode root){if(root==null)return;if(root.left==null&&root.right==null) {leafSize++;}getLeafSize(root.left);getLeafSize(root.right);}
子问题思路:
相当于求左子树的叶子结点+右子树的叶子结点。
public int getLeafSize2(TreeNode root){if(root==null)return 0;if(root.left==null&&root.right==null) {return 1;}return getLeafSize2(root.left)+getLeafSize2(root.right);}
获取第K层节点的个数
思路:第K层=左子树的第K-1层+右子树的第K-1层。
public int getKLevelNodeCount(TreeNode root,int k){if(root==null)return 0;if(k==1){return 1;}return getKLevelNodeCount(root.left,k-1)+getKLevelNodeCount(root.right,k-1);}
获取二叉树的高度
思路:左子树的高度和右子树的高度的最大值+1;
public int getHeight(TreeNode root){if(root==null)return 0;int leftHeight=getHeight(root.left);int righeHeight=getHeight(root.right);return leftHeight>righeHeight?leftHeight+1:righeHeight+1;}
检测值为value的元素是否存在
思路:前序遍历的方式进行遍历,先判断根节点是否相等,递归在判断左右节点。
public boolean find(TreeNode root, char key){if(root==null){return false;}//判断根节点是否相等if(root.val==key){return true;}boolean leftVal=find(root.left,key);if (leftVal==true){return true;}boolean rightVal=find(root.right,key);if (rightVal==true){return true;}return false;}
层序遍历
思路:判空,定义一个队列,循环入队入根节点,然后出队打印,带入左右孩子
public void levelOrder(TreeNode root){if(root==null){return;}//用队列完成Queue<TreeNode> queue=new LinkedList<>();queue.offer(root);while(!queue.isEmpty()){TreeNode cur=queue.poll();System.out.print(cur.val+" ");if(cur.left!=null){queue.offer(cur.left);}if(cur.right!=null){queue.offer(cur.right);}}}
2.返回List的每一层都是一个list
思路:利用二维数组方法,一层的放一起,利用循环,求出每层的大小,然后入队出队。
public List<List<TreeNode>> levelOrder2(TreeNode root) {List<List<TreeNode>> ret = new ArrayList<>();if (root == null) {return ret;}//用队列完成Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()) {//求当前队列的大小int size = queue.size();//用tmp存出里面的listList<TreeNode> tmp = new ArrayList<>();//出队4次while (size != 0) {TreeNode cur = queue.poll();tmp.add(cur);size--;if (cur.left != null) {queue.offer(cur.left);}if (cur.right != null) {queue.offer(cur.right);}}ret.add(tmp);}return ret;}
判断一棵树是不是完全二叉树
思路:在层序遍历的基础上,用两个循环写,先判空,然后根据队列入队出队,最后队列里应该全为空才是完全二叉树,所以第二个循环判断队列里的元素是否为空。
public boolean isCompleteTree(TreeNode root) {if (root == null) {return true;}Queue<TreeNode> queue = new LinkedList<>();queue.offer(root);while (!queue.isEmpty()) {TreeNode cur = queue.poll();if (cur != null) {queue.offer(cur.left);queue.offer(cur.right);}else {//此时遇到了nullbreak;}}//此时队列应该全为空while (!queue.isEmpty()){TreeNode cur=queue.poll();if(cur!=null){return false;}}return true;}