网站开发流程中有哪几个阶段,公司网站seo怎么做,thinphp 做外贸网站,蚂蚁网站建设从根节点往下分别查找左子树和右子树的最大节点#xff0c;再比较左子树#xff0c;右子树#xff0c;根节点的大小得到结果#xff0c;在得到左子树和右子树最大节点的过程相似#xff0c;因此可以采用递归的 //树节点结构 public class TreeNode { TreeNode left;…从根节点往下分别查找左子树和右子树的最大节点再比较左子树右子树根节点的大小得到结果在得到左子树和右子树最大节点的过程相似因此可以采用递归的 //树节点结构 public class TreeNode { TreeNode left; TreeNode right; int val; TreeNode(int val){ this.val val; } } public class Solution { /** * param root 传入根节点 * return 返回最大结果 */ public static TreeNode maxNode(TreeNode root){ //当再无子节点返回当前节点 if (root null) { return root; } TreeNode left maxNode(root.left);//递归得到左子树最大值 TreeNode right maxNode(root.right);//递归得到右子树的最大值 return max(root,max(left,right));//返回左节点根节点,右节点的最大值 } public static TreeNode max(TreeNode a,TreeNode b) { if (a null) { return b; } if (b null) { return a; } if (a.val b.val) { return a; } return b; } public static void main(String[] args) { TreeNode t1 new TreeNode(1); TreeNode t2 new TreeNode(-5); TreeNode t3 new TreeNode(3); TreeNode t4 new TreeNode(1); TreeNode t5 new TreeNode(2); TreeNode t6 new TreeNode(-4); TreeNode t7 new TreeNode(-5); t1.left t2;t1.right t5; t2.left t3;t2.right t4; t5.left t6;t5.right t7; System.out.println(Solution.maxNode(t1).val); } } 转载于:https://www.cnblogs.com/Darkqueen/p/9165794.html