求多叉树最大深度
class Solution {
public int maxDepth(Node root) {
if(root==null) return 0;
int y=0;
for(Node x : root.children)
{
y=Math.max(maxDepth(x),y);
}
return y+1;
}
}
二叉树的坡度
class Solution {
int sum=0;
public int findTilt(TreeNode root) {
if(root ==null) return 0;
sum=sum+cal2(root);
findTilt(root.left);
findTilt(root.right);
return sum;
}
public int cal2(TreeNode root)
{
int sum=0;
if(root ==null) return 0;
sum=Math.abs(cal(root.left)-cal(root.right));
return sum;
}
public int cal(TreeNode root)
{
int sum=0;
if(root ==null) return 0;
sum=sum+cal(root.left)+cal(root.right);
return sum+root.val;
}
}
多叉树的前序遍历
class Solution {
List<Integer> sum=new LinkedList<>();
public List<Integer> preorder(Node root) {
if(root==null) return sum;
sum.add(root.val);
cal(root);
return sum;
}
public List<Integer> cal(Node root) {
if(root==null) return sum;
for(Node x : root.children)
{
sum.add(x.val);
cal(x);
}
return sum;
}
}
多叉树的后序遍历
class Solution {
List<Integer> sum=new LinkedList<>();
public List<Integer> postorder(Node root) {
if(root==null) return sum;
cal(root);
sum.add(root.val);
return sum;
}
public List<Integer> cal(Node root) {
if(root==null) return sum;
for(Node x : root.children)
{
cal(x);
sum.add(x.val);
}
return sum;
}
}
二叉树的层平均值
class Solution {
public List<Double> averageOfLevels(TreeNode root) {
List<Double> cal=new LinkedList<>();
List<List<Double>> sum=new LinkedList<>();
Double temp;
if(root==null) return cal;
sum=level(root);
for(List<Double> x : sum)
{
temp=0.0;
for(Double y : x)
{
temp=temp+y;
}
temp=temp/x.size();
cal.add(temp);
}
return cal;
}
public List<List<Double>> level(TreeNode root)
{
List<List<Double>> sum=new LinkedList<>();
Queue<TreeNode> queue=new LinkedList<>();
if(root==null) return sum;
queue.add(root);
while(!queue.isEmpty())
{
List<Double> x=new ArrayList<>();
TreeNode temp;
int t=queue.size();
for(int i=0;i<t;i++)
{
temp=queue.poll();
x.add(Double.valueOf(temp.val));
if(temp.left!=null) queue.add(temp.left);
if(temp.right!=null) queue.add(temp.right);
}
sum.add(x);
}
return sum;
}
}
二叉搜索树节点的最小距离
class Solution {
TreeNode prenode=null;
int min=1000;
public int minDiffInBST(TreeNode root) {
if(root==null) return 0;
cal(root);
return min;
}
public void cal(TreeNode root)
{
if(root==null) return;
cal(root.left);
if(prenode!=null)
{
min=Math.min(root.val-prenode.val,min);
}
prenode=root;
cal(root.right);
}
}
递增顺序二叉树
class Solution {
TreeNode prenode=null;
int k=0;
TreeNode t;
public TreeNode increasingBST(TreeNode root) {
if(root==null) return null;
if(root.left==null&&root.right==null)
{
return root;
}
cal(root);
return t;
}
public void cal(TreeNode root)
{
if(root==null) return;
cal(root.left);
TreeNode x= new TreeNode(root.val);
if(prenode!=null)
{
prenode.right=x;
k++;
if(k==1)
{
t=prenode;
}
}
prenode=x;
cal(root.right);
}
}
二叉搜索树的范围和
class Solution {
int sum=0;
public int rangeSumBST(TreeNode root, int low, int high) {
if(root==null) return 0;
rangeSumBST(root.left,low,high);
if(root.val>=low&&root.val<=high)
{
sum=sum+root.val;
}
rangeSumBST(root.right,low,high);
return sum;
}
}
单值二叉树
class Solution {
int x;
int k=0;
public boolean isUnivalTree(TreeNode root) {
if(root==null) return true;
k++;
if(k==1) x=root.val;
if(root.val!=x)
{
return false;
}
return isUnivalTree(root.right)&&isUnivalTree(root.left);
}
}
前序遍历构造二叉搜索树
class Solution {
public TreeNode bstFromPreorder(int[] preorder) {
if(preorder==null||preorder.length<1) return null;
int[] inorder=new int[preorder.length];
for(int i=0;i<preorder.length;i++){
inorder[i]=preorder[i];
}
Arrays.sort(inorder);
return cal(preorder,0,preorder.length-1,inorder,0,inorder.length-1);
}
public TreeNode cal(int[] preorder,int ps,int pe,int[] inorder,int is,int ie)
{
if(ps>pe||is>ie) return null;
int curroot=preorder[ps];
int index=is;
while(index<=ie&&curroot!=inorder[index])
{
index++;
}
if(index>ie)
{
return null;
}
TreeNode node=new TreeNode(curroot);
node.left=cal(preorder,ps+1,pe-is+index,inorder,is,index-1);
node.right=cal(preorder,ps+1+index-is,pe,inorder,index+1,ie);
return node;
}
}
class Solution {
public TreeNode bstFromPreorder(int[] preorder) {
if(preorder==null||preorder.length<1) return null;
TreeNode root=null;
for(int i=0;i<preorder.length;i++)
{
root=cal(root,preorder[i]);
}
return root;
}
public TreeNode cal(TreeNode root,int curroot)
{
if(root==null) return new TreeNode(curroot);
if(curroot>root.val)
{
root.right=cal(root.right,curroot);
}
else
{
root.left=cal(root.left,curroot);
}
return root;
}
}
2021-09-05
最新推荐文章于 2024-05-22 18:44:11 发布