LeetCode 树的简单部分涉及的算法

LeetCode 树的简单部分涉及的算法
包括:
建立二叉排序树
建立平衡树
计算树的最大最小什么
前序遍历
和一些简单判断等。
代码:

package easy;
import easy.JavaTree;
import easy.TreeNode;
public class BinarySortTree {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//二叉排序树
//        int shuzu []= {3,1,2,5,0,7,9,8};
//        TreeNode root = new TreeNode(shuzu[0]);    //以3这个数为根节点
//        for( int i = 1 ; i < shuzu.length ; i ++){
//
//        SortBinaryTree(root, shuzu[i]);  
//        }
		//二叉平衡树
		int a []= {-2,-1,-3};
		int b []= {1,2,2,3,-1,-1,3,4,-1,-1,-1,-1,-1,-1,4};
        //preorder(createminheighttree(shuzu, 0, shuzu.length -1));
		//TreeNode root=createminheighttree(shuzu, 0, shuzu.length -1);
		TreeNode root=JavaTree.createTree(a);
		System.out.println(height2(root));
		System.out.println(isBalanced(root));
		System.out.println(hasPathSum(root, -5));

	}
	//递归数组从头到尾建立二叉树,可能不平衡
	public static TreeNode SortBinaryTree(TreeNode node,int i){
		 
		 
		if(node == null){
			node = new TreeNode(i);
			return node;
		}
		else{
			if(i <= node.val){
 
				node.left =  SortBinaryTree(node.left, i);
			}
			else{
				node.right = SortBinaryTree(node.right,i);
			}
			
			return node;
		}
 
	}
	//建立平衡二叉排序树
	 public  static TreeNode  createminheighttree(int a [] , int start  , int end ){
		 
		   if(end < start){
		     return null;
		   }
		   int min = (start + end) / 2;
		   TreeNode  root = new TreeNode(a[min]);
		   root.left = createminheighttree(a, start, min -1);
		   root.right = createminheighttree(a, min+ 1, end);
		   return root;
	  }
	 //判断是否是平衡二叉树
	 public static boolean isBalanced(TreeNode root) {
		 if(root==null)
		     return true;
		 //System.out.println(height(root.left.left)+"          "+height(root.right)+"        "+height(root));
		 if(Math.abs(height(root.left)-height(root.right))>1)
		 {
			 return false;
		 }
		 return isBalanced(root.left)&&isBalanced(root.right); 
		 //return true;
	        
	    }
	 //树深度
	 public static int height(TreeNode root)
	 {
		 if(root==null)
			 return 0;
		 else
		 {
			 int a=height(root.left);
			 int b=height(root.right);
			 return (a>b?a:b)+1;
		 }
	 }
	 //求树的最小高度
	 public static int height2(TreeNode root)
	 {
		 if(root==null)
			 return 0;
		 else if(root.left==null&&root.right==null)
			 return 1;
		 else if(root.left==null||root.right==null)
			 return 1+height2(root.left)+height2(root.right);
		 else
		 {
			 int a=height2(root.left);
			 int b=height2(root.right);
			 return (a>b?b:a)+1;
		 }
		 
	 }
	  //路径总和
	
	 public static boolean hasPathSum(TreeNode root, int sum) {
		 int n=0;
		 if(root==null)
			 return false;
		else if((n+root.val==sum)&&(root.left==null&&root.right==null))
			 return true;
		 else if((n+root.val==sum)&&(root.left==null||root.right==null))
			 return false;
		 else
		     return hasPathSum(root.left,sum-root.val)||hasPathSum(root.right,sum-root.val);
		   
	    }
	 

	public static void preorder(TreeNode root){
		if(root!=null){
			System.out.println(root.val);
			preorder(root.left);
			preorder(root.right);
		}
		
	}


}

注:上面创建普通二叉树函数可以参考另一篇博客《Java创建普通二叉树》

猜你喜欢

转载自blog.csdn.net/qq_30122883/article/details/82955743