算法合集(五)

这一篇内容其实很久之前就已经开始动手了,但是由于这期间手上有一些项目需要忙,所以一直延误到现在,趁端午回家前赶紧写完。

三角形的最大周长

给定由一些正数(代表长度)组成的数组arr,返回由其中三个长度组成,面积不为0的三角形的最大周长。
如果不能形成任何面积不为0的三角形,则返回0。

实现

首先应该知道组成三角形的边长的条件,即任意两边之和必须大于第三边,任意两边之差必须小于第三边。在这里用到的是最长边一定大于其他两边的和。
思路:首先将数组进行排序,然后从最大的一位开始向前取三个数字来判断是否可以组成三角形,如果可以则直接返回三边之和。

package test;

import java.util.Arrays;

public class MaxPerimeter {
	
	public static int maxPerimeter(int[] arr) {
		//排序
		Arrays.sort(arr);
		//从后往前依次找,贪心
		for(int i = arr.length - 1; i >= 2; i--) {
			if(arr[i-1] + arr[i-2] > arr[i]) {
				return arr[i-1] + arr[i-2] + arr[i];
			}
		}
		return 0;

	}
	public static void main(String[] args) {
		int[] arr = new int[] {3,6,2,3};
		System.out.println(maxPerimeter(arr));
	}
}

二叉树遍历

关于前序、中序以及后序的算法思想不多赘述,懂的都懂。
另外,使用迭代的方式实现递归函数,与递归等价。

前序遍历

递归

package test;

public class BinaryTree {
	public static class TreeNode{
		int val;
		TreeNode left;
		TreeNode right;
		int deep;
		public TreeNode() {}
		public TreeNode(int val) {this.val=val;}
		public TreeNode(int val,TreeNode left,TreeNode right) {
			this.val = val;
			this.left = left;
			this.right = right;
		}
		
		
	}
	
	public static void main(String[] args) {
		TreeNode node7 = new TreeNode(7,null,null);
		TreeNode node6 = new TreeNode(6,null,null);
		TreeNode node5 = new TreeNode(5,node6,node7);
		TreeNode node4 = new TreeNode(4,null,null);
		TreeNode node3 = new TreeNode(3,null,null);
		TreeNode node2 = new TreeNode(2,node4,node5);
		TreeNode node1 = new TreeNode(1,node2,node3);
		preorder(node1);
	}
	
	public static void preorder(TreeNode root) {
		//如果为null则直接返回
		if(root == null) {
			return;
		}
		//将node1入栈并打印
		System.out.println(root.val);
		preorder(root.left);
		preorder(root.right);
	}
}

迭代

    //前序遍历 迭代
	public static void preorder2(TreeNode root) {
		if(root != null) {
			Stack<TreeNode> stack = new Stack<TreeNode>();
			stack.add(root);
			while(!stack.isEmpty()) {
				root = stack.pop();
				if(root != null) {
					System.out.println(root.val);
					//先入右节点再入左节点,后入先出
					stack.add(root.right);
					stack.add(root.left);
				}
				
			}
		}
	}

中序遍历

递归

    //中序遍历 第二次成为栈顶元素即打印
	public static void Inorder(TreeNode root) {
		//如果为null则直接返回
		if(root == null) {
			return;
		}
		Inorder(root.left);
		System.out.println(root.val);
		Inorder(root.right);
	}

迭代

    //中序遍历 递归
	public static void Inorder2(TreeNode root) {
		if(root != null) {
			Stack<TreeNode> stack = new Stack<TreeNode>();
			while(!stack.isEmpty()||root!=null) {
				if(root != null) {
					stack.push(root);
					root = root.left;
				}else {
					root = stack.pop();
					System.out.println(root.val);
					root = root.right;
				}
				
			}
		}
	}

后序遍历

递归

    //后序遍历 第三次成为栈顶元素即打印
	public static void Postorder(TreeNode root) {
		//如果为null则直接返回
		if(root == null) {
			return;
		}
		Postorder(root.left);
		Postorder(root.right);
		System.out.println(root.val);
	}

*迭代

    //后序遍历 迭代
	public static void Postorder2(TreeNode root) {
		if(root != null) {
			Stack<TreeNode> stack = new Stack<TreeNode>();
			TreeNode prev = null;
			while(!stack.isEmpty()||root!=null) {
				//找到最左边的子节点
				while(root != null) {
					stack.push(root);
					root = root.left;
				}
				root = stack.pop();
				if(root.right == null || root.right == prev) {
					System.out.println(root.val);
					prev = root;
					root = null;
				}else {
					stack.push(root);
					root = root.right;
				}
			}
		}
	}

层序遍历

递归

    //层序遍历
	public static void levelOrder(TreeNode root, int i,  ArrayList list) {
		if(root == null) {
			return;
		}
		//防止下标越界
		int length = list.size();
		if(length <= i) {
			for(int j = 0; j <= i - length; j++) {
				list.add(length + j, null);
			}
		}
		list.set(i, root.val);
		levelOrder(root.left,2*i,list);
		levelOrder(root.right,2*i+1,list);
	}

迭代

    //层序遍历 迭代
	public static void levelOrder2(TreeNode root) {
		Queue<TreeNode> q = new LinkedList<>();
		q.add(root);
		while(!q.isEmpty()) {
			TreeNode node = q.poll();
			if(node!=null) {
				System.out.println(node.val);
				q.add(node.left);
				q.add(node.right);
			}
		}
	}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值